We, humans, tend to put us above everything and to believe that animals have sex just for reproduction.
This is not like that, and starting with a point in the evolution, when a certain level of intelligence was achieved by the mammals, the animals were also able to predict events, including some "naughty activities" that deliver them pleasure.
"This is something which does not happen amongst reptiles, birds and fish, because there is a relation between the development of the neocortex (this is the site of mammalian intelligence, including human), a brain zone characteristic for mammals, and masturbation" explained Tito Varela, head of Animal Biology Department at the Santiago de Compostela University, Spain.
Well, I'm gonna retell you an event I witnessed years ago. I was at a Zoo, standing in front of a large cage exposing Hamadryas Baboons. The animals were coming in front of the cage bagging for food, being especially interested in fresh grass shoots (it was early spring and the animals probably just wanted some green stuff to ingest).
At a certain moment, I noticed the young male standing in front of me had started to rub his pencil-like penis which was in erection. It was incredible: that animal was simply masturbating, indifferent at his surroundings! In fact, in that cage there was a huge dominant male, some female with its offspring and a few young males. The young males were obviously forbidden to copulate: the big male was very alert, chasing them away from the females. So, they found a way to discharge their sexual drive.
Primates, at least those highly evolved (monkeys and apes) regularly masturbate! 'Cause primates not only possess the brain, but are also very handy¬
Monkeys from South America (like spider monkeys and howler monkeys), that possess prehensile tails, use the tail, too, with this purpose. Apes (gorillas, orangutans, chimpanzees, bonobo) use to caress their penis with their hands, feet and even their mouth! Bonobos, also wrongly named dwarf chimpanzees (they are indeed closely related to chimpanzees, but are by no means smaller), frequently masturbate and practice sexual orgies, in which individuals of all ages participate. And in bonobo, females masturbate (photo above), too, which is very unusual.
But not only primates masturbate. Elephants were observed making use of their trunks for this purpose.
And dolphins rubbing themselves against the seabed.
This should not amaze us at all, as cetaceans (whales and dolphins) and elephants are considered to possess the most developed neocortex, along with the primates (in other words, these three groups are considered the most intelligent mammals).
One puzzle: some birds are also proven to be quite intelligent, even if bird intelligence does not reside in a neocortex, but in other brain zone. So, why they do not masturbate?
To begin with, birds do not possess a penis (except ostriches, ducks, and geese, but these groups are not exactly among the so-called "intelligent birds").
But even so, captive male parrots were seen rubbing their cloaca (which is used for mating) against objects (including human hands) and ejaculating, when they are not paired with females.
Photo bellow: black Celebes macaques