Background. Sleep is a biological enigma. Despite occupying much of an animal's life, and having been scrutinized by numerous experimental studies, there is still no consensus on its function. Similarly, no hypothesis has yet explained why species have evolved such marked variation in their sleep requirements (from 3 to 20 hours a day in mammals). One intriguing but untested idea is that sleep has evolved by playing an important role in protecting animals from parasitic infection. This theory stems, in part, from clinical observations of intimate physiological links between sleep and the immune system. Here, we test this hypothesis by conducting comparative analyses of mammalian sleep, immune system parameters, and parasitism. Results. We found that evolutionary increases in mammalian sleep durations are strongly associated with an enhancement of immune defences as measured by the number of immune cells circulating in peripheral blood. This appeared to be a generalized relationship that could be independently detected in 4 of the 5 immune cell types and in both of the main sleep phases. Importantly, no comparable relationships occur in related physiological systems that do not serve an immune function. Consistent with an influence of sleep on immune investment, mammalian species that sleep for longer periods also had substantially reduced levels of parasitic infection. Conclusion. These relationships suggest that parasite resistance has played an important role in the evolution of mammalian sleep. Species that have evolved longer sleep durations appear to be able to increase investment in their immune systems and be better protected from parasites. These results are neither predicted nor explained by conventional theories of sleep evolution, and suggest that sleep has a much wider role in disease resistance than is currently appreciated.