Over 1,500 animal species, from bonobos to sea urchins to penguins are known to engage same-sex sexual behavior. Still, scientists don’t understand exactly how it came to be or why it happens. While some say the behavior might have existed since the animal kingdom first arose more than half a billion years ago, it may have actually evolved repeatedly in mammals. A study published October 3 in the journal Nature Communications suggests that the behavior possibly plays an adaptive role in social bonding and reducing conflict, and evolved multiple times.

[Related: A massive study confirms no one ‘gay gene’ controls sexual preference.]

The behavior is particularly prevalent in nonhuman primates. It has been observed in at least 51 species from small lemurs up to bigger apes. For one population of male macaques, same-sex sexual behavior may even be a common feature of reproduction and is related to establishing dominance within groups, handling a shortage of different-sex partners, or even reducing tension following aggressive behavior. 

In this new study, the team from institutions in Spain surveyed the available scientific literature to create a database of records of same-sex sexual behavior in mammals. They traced the behavior’s evolution across mammals and tested for any evolutionary relationships with other behaviors. 

The team found that same-sex sexual behavior is widespread across mammal species, occurs in similar frequency in both males and females, and likely has multiple independent origin points. This analysis found that the behavior helps establish and maintain positive social relationships in animals including chimpanzees, bighorn sheep, lions, and wolves.

“It may contribute to establishing and maintaining positive social relationships,” study co-author José Gómez told The New York Times. “With the current data available, it seems that it has evolved multiple times.” Gómez is an evolutionary biologist at the Experimental Station of Arid Zones in Almería, Spain. 

Importantly, they caution that the study should not be used to explain the evolution of sexual orientation in humans. This research focused on same-sex sexual behavior defined as short-term courtship or mating interactions, instead of a more permanent sexual preference. 

Additionally, male same-sex sexual behavior was likely evolved in species with high rates of male adulticide–-when adult animals kill other adults. The team believes that this suggests the behavior may be an adaptation meant to mitigate the risks of violent conflict between males.

Harvard University primatologist Christine Webb, who did not participate in the study, told The Washington Post that the findings add to other research and widen the scope of what it means for a behavior to be considered adaptive.

[Related: Same-sex mounting in male macaques can help them reproduce more successfully.]

“This general question of evolutionary function—that behavior must aid in survival and reproduction—what this paper is arguing is that reaffirming social bonds, resolving conflicts, managing social tensions, to the extent that same-sex sexual behavior preserves those functions—it’s also adaptive,” Webb said. 

Webb also added that it makes sense that other animals would have sex for a variety of reasons the way that humans do.

The authors caution that these associations could also be driven by other evolutionary factors. Same-sex sexual behavior has also only been carefully studied in a minority of mammal species, so our understanding of the evolution of same-sex sexual behavior may continue to change as more mammalian species are studied.