Due to the peculiarities of muscle and tendon functioning.
During rest, animals usually take a position that allows them to relax their muscles as much as possible. However, for most birds, a peaceful sleep is only possible with the tension of the leg muscles, which provide stability and help maintain balance on a branch or perch. These muscles are connected to the toes by long tendons. When a bird sits down, the muscles contract, the tendons stretch, and the toes grip.
While sleeping, a bird cannot straighten its legs (its body is immobile), so the toes continue to hold onto the support. Upon waking, the animal lifts its body, the tendons relax, and the lock “unclamps.”
Birds that sleep standing in water (such as flamingos or herons) often tuck one leg while sleeping. This helps them retain heat by giving less warmth to the water and maintaining an optimal body temperature for a longer time.
However, sleeping while standing is not the only way for birds to rest. Storks can alternate sleeping while flying in the air during migrations. Meanwhile, the black tern, after leaving its nest, can fly continuously over the sea for several years: its legs are not designed for landing on water, and it catches aquatic insects and small fish while flying low, sleeping “on the wing.”