“Pandiculation is the involuntary stretching of the soft tissues, which occurs in most animal species and is associated with transitions between cyclic biological behaviors, especially the sleep-wake rhythm (Walusinski, 2006). Yawning is considered a special case of pandiculation that affects the musculature of the mouth, respiratory system and upper spine (Baenninger, 1997). When, as often happens, yawning occurs simultaneously with pandiculation in other body regions (Bertolini and Gessa, 1981; Lehmann, 1979; Urba-Holmgren et al., 1977) the combined behavior is referred to as the stretch-yawning syndrome (SYS).”
Involuntary stretching (of various body regions) very commonly occurs across a wide range of animals - mammals, reptiles and even fish. It's associated with tiredness, as it's frequently seen before and after sleeping episodes. The reasons for its deep-rooted prevalence (and its precise functions) are unknown.
It appears to be under some kind of reflex control, as (in humans) those who have been partially paralysed (stroke etc) are often observed to move their affected limb whilst yawning/stretching - but are are unable to do so under voluntary control.
As the paper cited above notes, stretching is deeply associated with Yawning