Biologists Young-Hui Chang and Lena Ting had an epiphany while studying a dead flamingo. There was nothing about their anatomy that gave them the extraordinary ability to balance on one leg for hours at a time. He picked up the dead bird by its leg and, bizarrely, the leg stood upright just as if it were alive.
Standing on one leg “is a challenging yoga posture, and a test of coordination that people use,” says Ting. To maintain our balance, we constantly use our muscles to make tiny adjustments to our posture. Flamingos have no such problem. When they raise a leg, their body weight shifts in a way that naturally stabilizes the joints of their standing limb, so they can remain upright without any muscular activity. They can sleep like that. And as Chang and Ting found, they can even keep balanced when dead. You can pose a flamingo cadaver on one leg, and leave it there.
To understand how a bird can balance on one leg, you have to know that bird legs are not how we humans normally think of them. The upright part that supports the flamingo are analogous to our shins and feet. The thigh and knee are hidden under the feathers, and provide a platform of sorts for the flamingo to sit on. The explanation is at The Atlantic, where Ed Yong helpfully draws on a photograph to make it clear.