Not every behavior needs to confer a reproductive advantage, dammit!
Despite Daniel Enger’s valiant effort:
Despite this bestiary of autoeroticism, scientists have spent relatively little time on the question of why animals might have evolved to masturbate. At first glance, the behavior would seem to be maladaptive. First, there’s all the energy that’s wasted on the production of spilled seed—macaques, for example, are thought to devote between 1 percent and 6 percent of their daily metabolism to the production of ejaculate. Second, it distracts the animal from the more important work of finding food and evading predators, let alone mating. According to the literature on horses, a masturbating stallion sometimes takes on “a trance-like, glazed-eye appearance.” What could be more inviting to a hungry bear?
I still think this sounds like spandrel territory:
Still, neither the fresh-sperm hypothesis nor its discredited cousin, the kamikaze-sperm hypothesis, can account for more than a small subset of animal masturbation. Reloading might explain the behavior of bucks, bulls, and male primates, all of which tend to ejaculate at the end of an autoerotic episode. But many other animals never reach that point. Horses rarely climax, despite masturbating dozens of times per day—so what motivates the dalliance of a stallion or, for that matter, a mare? Can evolution account for female masturbation in the animal kingdom?
Just think about it for a second.
For these behaviors to be selected out of a population we would need a genetic mutation that prevented animals from masturbating but also still allowed them to take pleasure from genital sex. Like some sort of magic “anti-onanism” gene that makes it only feel good if someone else touches it.
So yeah, I say “spandrel.”
Image Source: https://daily.swarthmore.edu/2009/2/19/gilbert-vatican/