Humans didn't invent masturbation. We get the urge because our ancestors did too, even back to the earliest mammals and reptiles. It's likely that as soon as animals evolved brain circuitry that made stimulating sexual organs rewarding, individuals started taking the opportunity to reward themselves.
The diversity of animals that have been caught taking a bit of alone time goes far, far beyond dogs humping legs. Here's a sample.
It should be no surprise to anyone that our primate cousins are champion masturbators. They have hands, after all. The art of self-pleasure has been observed in males from about 80 species from ape to monkey to lemur, making it one of the most common and widespread primate sexual behaviours. Females from at least 50 species also get into the act, and they can get creative: for example, female orangutans and capuchin monkeys have both been observed using sticks and other plant parts as makeshift dildos.
They don't have hands, but male and female cetaceans -- at least in the smaller species -- manage to masturbate anyway, mostly by rubbing their genitals on things. The sea floor, the walls of tanks (if they're in captivity), other animals, any firm objects they find in their environment: it's all fair game, though the male bottlenose who reportedly wrapped a live eel around his penis seems particularly inventive.
Masturbation's been seen in bottlenose and spinner dolphins, killer whales, and two types of river dolphin. No one knows whether the bigger whales masturbate, or what it'd look like if they did.
A male Asian elephant has a long, hefty penis that he can move around with a set of enlarged muscles at its base. That lets him navigate it into a female's vagina, or masturbate by repeatedly striking his erect penis against his belly. Elephants are most interested in beating themselves when they're in the early stages of musth, the season of peak testosterone that leaves them aggressive, dribbling urine, and irresistibly sexy to female elephants. It doesn't last forever: they lose interest as the musth goes on.
What is it with the big blubbery handless animals? Male walruses will rub their front flippers over the shaft of their alarmingly large penis. Or they will auto-fellate, because it's just that big.
Rodents jack off, too. During the breeding season, both male and female porcupines rub sticks against their genitals. Male Cape ground squirrels masturbate at any time of the year, but dominant males do it most often, paradoxically, right after they have copulated. University of Manitoba biologist Jane Waterman, who first quantified this behaviour, suggests that because the males and females have many mates over the breeding period, jerking off immediately after sex might keep males from picking up and spreading sexually transmitted infections.
Young male vampire bats and male fruit bats (as in this video) have both been seen pleasuring themselves with their tongues. Typically, an animal will groom and lick his penis, sometimes achieving orgasm. Since they rest hanging upside down by their feet, that can get a bit messy.
Male lizards have a doubled phallus (their hemipenes) which goes far beyond the fancy glans we see in some marsupials: each one has its own sperm channel, and the animals can use them independently -- swapping sides each time they copulate. Many species also rub their hemipenes against the ground regularly, sometimes daily when it's the breeding season.
These rounds of rubbing may have a purpose -- one study of the behaviour suggested that pulling off the top layer of hemipenis skin may help remove any bacteria and parasites the male picked up during copulation, or keep the intricate skin frills on the surface of the hemipenes well groomed.
For marine iguanas, masturbation can serve a much more direct reproductive role. All males, large and small, will try their luck at mating. But large males are territorial, and ruthless about shoving small males off of females mid-copulation long before they ejaculate. For the small males, masturbation is a strategy: they finish off on the rocks, ejaculate into their cloacal folds, and store the semen so it's ready to push inside a female first thing the next time they get a chance to mate. The trick improves their fertilization success by 41%, and passes the behaviour to the next generation.
Male turtles start masturbating as soon as they're sexually mature, pushing their terrifyingly large and ornate organs against hard objects, sometimes squeaking with what sounds like delight. Or maybe they're confused and think that rocks and porch steps are unusually unresponsive female turtles.
It's the breeding season, you're a young male Adélie penguin, and you haven't been able to find a willing mate? No problem: the bare rock of the rookery will do.
The auto-erotic behaviour of Adélie penguins was first observed by G. Murray Levick in 1911 during the Scott Antarctic expedition:
Sometimes we saw these birds, after walking some distance, apparently in the vain search for hens, stand motionless and rigid upon the ground, then stiffening themselves, assume the attitude and go through the motions characteristic of the sexual act, in some cases actually ejecting their semen on to the ground.
He found the behaviour (along with the necrophilia, sexual coercion, and chick abuse he observed) so shocking that he simply left it out of his seminal monograph on the species. He wrote up his observations on the sexual habits of the "hooligan cocks" separately, in Greek so that no one other than professional ornithologists would be tainted by the knowledge. The unpublished description was only rediscovered a few years ago. [Thomsen and Sommer 2015] [Morisaka et al. 2013] [Norris 1977] [Jainudeen et al. 1971] [Shadle 1946] [Waterman 2010] [Greenhall 1965] [den Bosch 2001] [Wikelski and Baurle 1996] [Rit et al. 2010] [Russell et al. 2012] Top image (primate) by Kevin Botto via Flickr [CC BY-ND 2.0] Gibbon by Linda Tanner via Flickr [CC BY 2.0] Beluga whales by Brian Gratwicke via Flickr [CC BY 2.0] Asian Elephant by Adhi Rachdian via Flickr [CC BY 2.0] North American porcupine by Gary Eslinger/USFWS via Flickr [CC BY 2.0] Marine iguana by Lieutenant Elizabeth Crapo, NOAA Corps via Flickr [CC BY 2.0] Adélie penguin by Liam Quinn via Flickr [CC BY-SA 2.0]