For the first three months of my life, I survived only on my mother's breastmilk. Quite possibly you did too.

Feeding babies on milk is so universal, it's hard to imagine what life would be like if we didn't do it. But out in the natural world, milk is common but not universal. Entire groups of animals, birds for example, don't produce anything like it and survive perfectly well.

So how and why did milk evolve? When scientists began to trace its evolutionary history, they found it is far older than we might have thought. Milk dates back hundreds of millions of years to the first animals that walked on land; although the first milk might not have been much like the stuff you put on your cereal.

Milk and breasts gained star status in the 18th century, when the Swedish biologist Carolus Linnaeus began categorizing species into groups based on their unique traits.

Some of Linnaeus's groupings have proved to be wrong, but one has held firm. He realized that a host of animals, including humans, all lactate. Females of these species nourish their young with milk from their mammary glands.

Linnaeus called these animals the Mammalia, meaning "of the breasts". Nowadays we tend to call them "mammals", and they really do all produce milk.

Hooded seals produce milk for their pups that is probably fattier than the richest ice cream. Ocean-dwelling whales and dolphins breastfeed their young, flying bats make milk, and so do dogs, cats, elephants, rhinos and humans.

This rule even extends to the most primitive mammals, which don't give birth to live young.

The monotremes, which include the duck-billed platypus and spiny anteaters, all lay eggs. But they still have milk-producing mammary glands.

The same is true of marsupials like kangaroos and possums. They give birth to tiny babies, which then live in a special pouch on the mother and grow by suckling her milk.

Based on all this, it seems clear that milk and breastfeeding are unique to the mammals. But it's not obvious when it evolved.

It's difficult to study the evolution of breastfeeding. Unlike bones, mammary glands do not fossilize.

"It's hard to get any prehistorical data from the mammary gland," says Peter Hartmann of the University of Western Australia in Perth.

He believes that mammary glands were fully evolved before the first mammals set foot on Earth

So scientists sought answers elsewhere. They scanned the genes of animals that produce milk, and compared them with animals that do not.

They also looked at the structure of mammary glands in different milk-producing animals, compared the composition of their milk, and examined how they reproduce.

These studies have led Olav Oftedal of the Smithsonian Environmental Research Center in Edgewater, Maryland to a surprising conclusion. He believes that mammary glands were fully evolved before the first mammals set foot on Earth.

"Even though we now consider lactation as a characteristic of mammals, and it is clear that we are the only living existing creatures that have mammary glands, I believe that mammary glands have an older origin," Oftedal says.

He traces the beginnings of mammary glands back to the first animals that made their way to land from water. Some of these early land animals evolved into amniotes: backboned, four-limbed animals that eventually gave rise to reptiles, birds and mammals.

Like their fishy ancestors, the amniotes laid eggs. But these eggs were adapted to survive on land. They had fibrous and porous eggshells, and specialized membranes that allowed easy exchange of oxygen and carbon dioxide between the embryo and the surrounding air.

But there was a problem. The porous eggshells left the eggs at the mercy of the weather. If the temperature got too hot or the weather too dry, the eggs could dry out quickly. So the amniotes had to find a way to protect them.

The ancestors of reptiles and birds developed hard, calcified and waterproof eggshells, like those of chicken eggs. These waterproof eggs were less prone to lose moisture.

It would have been easy for the baby animals to start eating these secretions

But the ancestors of mammals kept making porous eggs. Instead of hardening their eggshells, Oftedal says, they took advantage of the porous eggshells to transfer extra water and nutrients into the eggs.

Oftedal argues that these mammal-like amniotes could have begun secreting water from simple glands in their skin. The water would have passed into the eggs through the pores, to keep them moist and safe.

They could also have secreted other useful chemicals, such as nutrients or antimicrobials. Once that was established, it would have been easy for the baby animals to start eating these secretions even after they had hatched from their eggs.

Frogs offer a clue to how this might have worked.

Some frogs lay eggs on dry land, like the male coqui (Eleutherodactylus coqui). They then huddle with their eggs, and transfer water to them through direct body contact or skin secretions.

In some other amphibians – like the worm-like caecilians – the skin of females thickens with nutritious, fat-rich deposits. Once the tiny hatchlings come out of the eggs, they scrape this nutrient-rich skin off using specialised teeth.

In all these cases, the parents are actively transferring nutrients to their young through skin secretions. Oftedal argues they are comparable to breastfeeding.

Somehow, these watery secretions must have evolved into the complex, fatty milk that mammals make today.

As time went on, hatchlings may have started to rely more on the nutrients and protective antimicrobial components secreted by their mother's glands, and less on the nutrients inside their egg yolk.

This timeline can be traced within the genes of mammals and their ancestors

Meanwhile, the simple skin glands of the mammalian amniotes could have given rise to the intricate mammary glands of modern mammals.

The watery secretions, first used simply to wet the eggs, could have gained ever more useful chemicals, until they became the main source of nutrition and protection for babies. Oftedal argues that all this happened before true mammals appeared.

This timeline can be traced within the genes of mammals and their ancestors.

Many components of milk have an ancient origin. We know this because many milk-related genes are older than the mammals.

Take the caseins, which are usually the most abundant protein in mammalian milk. They help in transporting nutrients like calcium and phosphorus to babies, which helps the babies grow their skeleton and tissues.

Researchers have found that all mammals have highly organised clusters of genes, which code for three main types of caseins.

From this we can deduce that milk caseins are ancient. Oftedal believes they diverged into the three main types that we see today long before the early mammals separated into monotremes, marsupials and placental mammals.

Slowly, milk caseins went from being a nutrient supplement to egg yolk, to a major source of nutrients for babies.

Researchers have also traced how mammals became less dependent on the nutrients in egg yolk.

In marsupials and placental mammals, all three vitellogenin genes are turned off

About 170 million years ago, important egg yolk proteins called vitellogenins began disappearing one by one, according to a 2008 study. Again, this was before true mammals walked on earth.

All modern birds and reptiles have three genes associated with the production of vitellogenins. Egg-laying mammalian ancestors also had three genes.

But among living mammals, only the egg-laying monotremes have one functional vitellogenin gene, alongside two inactive ones. In marsupials and placental mammals, all three vitellogenin genes are turned off.

The mammals would only have turned off these genes if they had substitutes to hand. So there must have been an alternative source of nutrients available, such as casein, before the vitellogenins were deactivated, argues Oftedal.

If milk production as an ability is so old, why do we see it only in mammals today?

If egg yolk proteins began disappearing long before mammals appeared, it suggests that milk was already the chief source of nutrients for mammals' egg-laying ancestors.

The basic composition of milk was most likely set before mammals evolved, Oftedal says. "But after mammals evolved as a group and went down different evolutionary paths, milk continued to change, more in terms of the relative proportions of its different constituents."

But if milk production as an ability is so old, why do we see it only in mammals today?

The simplest explanation is that all the pre-mammalian lineages went extinct, leaving mammals as the only surviving milk-dependent group, says Oftedal.

It may seem odd that milk production never evolved again. But actually a few non-mammalian animals do produce milk-like fluids.

"True" milk-producing mammary glands are unique to mammals

For example, some cockroaches produce a protein-rich milky fluid, which their embryos feed on. The Pacific beetle cockroach (Diploptera punctata) is one.

Some birds, including pigeons, doves, flamingoes and emperor penguins, produce thick, nutritious milk-like fluids in sacs near their throats. In some cases, even males can produce this "milk", and regurgitate it into the mouths of their hatchlings.

These substances are used in the same way as mammalian milk, to nourish the animals' underdeveloped young. But they differ in their chemical makeup and in the way they are produced.

So "true" milk-producing mammary glands are unique to mammals. But even within mammals, different species lactate in different ways.

The mammary glands in monotremes like the duck-billed platypus are very different from the breasts of other mammals.

Humans are fixated on nipples, as the singer Janet Jackson is painfully aware

They secrete milk through a network of ducts onto the surface of their bellies. There they have two flat mammary patches, without nipples, covered by fur. The hatchlings that come out of the small eggs are tiny and cannot seek out food independently. So they latch onto these simple mammary glands and feed on milk.

All other mammals, whether they are marsupials like kangaroos or placental mammals like us, have nipples.

Humans are fixated on nipples, as the singer Janet Jackson is painfully aware. But they may have a rather prosaic origin. According to Oftedal, nipples may have evolved to help avoid infections.

When monotremes release milk onto their skin, bacteria could easily colonise it. That risks infecting both the mother and her offspring, he says.

The babies of placental mammals get additional nutrients while in the womb

"So one advantage of nipples is that it allows the direct transfer of milk from mammary glands into the mouths of the young, without being exposed to the environment," says Oftedal.

Even among mammals that have breasts with nipples, lactation differs greatly.

This is partly because the babies of placental mammals get additional nutrients while in the womb, via an organ called the placenta that connects the foetus to its mother. This extra supply of nutrients allows placental babies to stay in the womb for longer than marsupial babies.

Marsupial young, on the other hand, almost entirely depend on their mother's milk for survival. As a result, marsupials tend to lactate for a long time, until their young are big enough to feed independently.

Because breastfeeding is such an intimate, emotional experience, it is easy for us to forget that it is not a purely human process.

Thanks to modern genetics, we can now begin to tell the story of its evolutionary origins.

If Oftedal is right, it came about because land animals faced a series of challenges in caring for their young. First they had to keep their eggs moist, then they had to supplement their offspring's food, and finally they had to get food into their offspring without the risk of infection.

In short, lactation is a primal behaviour, and its origins lay hundreds of millions of years in the past. A mother breastfeeding her child today is the latest link in an unbroken chain of lactating parents, stretching back to the first animals to ever walk on land.