The uneven distribution of biodiversity on Earth is one of the most general and puzzling patterns in ecology, and many hypotheses have been proposed to explain it. However, previous studies investigating these hypotheses have been mainly descriptive as controlled experiments are hardly feasible at such large geographical scale. Here, we use bird migration – the seasonal redistribution of about 20% of bird species across the world – as a natural experiment for testing the species-energy relationship, the hypothesis that animal diversity is driven by energetic constraints. We develop a mechanistic model of bird distributions across the world and across seasons based on simple ecological and energetic principles reflecting Lotka's maximum power principle. Using this model, we show that bird species distribute as to optimise the balance between energy acquisition and energy expenditure while taking into account competition with other species. These findings support, and provide a mechanistic explanation for, the species-energy relationship. They also provide a general explanation of migration as a mechanism allowing birds to optimise their energy budget in the face of seasonality and competition. Finally, our mechanistic model provides a tool for predicting how ecosystems will respond to global anthropogenic change, and we used it to reconstruct the global seasonal distribution of birds back to the Last Glacial Maximum.