An ancient Egyptian mystery has been solved, according to researchers, who say they have cracked the conundrum of where millions of mummified birds came from.

Pharaohs and members of the nobility were often mummified, but the practice was not reserved for humans – cats, crocodiles, mice and mongooses are among the mummified animals that have been found.

While some have been discovered alongside human burials, others – most notably the sacred ibis bird – were mummified as part of rituals designed to curry favour with the gods.

More than 4 million sacred ibis mummies have been found in the catacombs of Tuna el-Gebel and 1.75 million have been discovered in the ancient burial ground of Saqqara. The vast majority were votive offerings to the god Thoth, a practice that had its heyday between 450BC and 250BC.

“The ibis was considered [to represent] the god Thoth, the god of wisdom, the god of magic, the god of judgment, writing all sorts of things,” said Sally Wasef, a research fellow at Griffith University in Australia and first author of the research.

“If you had a boss that annoys you and you don’t feel like you are getting a good judgment from him or you want fairness and justice, you go and ask Thoth to interfere and in return you promise to offer him an ibis, a mummified ibis, in his annual feast.”

But the sheer quantity of mummified ibises left experts scratching their heads – where did all these birds come from?

One suggestion is that they were reared on an industrial scale in hatcheries. That idea appears to have some support in ancient texts, such as the writings of Hor of Sebennytos, a priest and scribe in the second century BC, who wrote about feeding tens of thousands of sacred ibis with bread and clover.

To explore the possibility, Wasef and colleagues analysed DNA from 14 mummified sacred ibises found in ancient Egypt and 26 modern samples from across Africa.

As DNA breaks down over time, the team focused on DNA found within the “powerhouses” of cells, known as mitochondria, rather than the DNA in the nucleus of cells that forms the chromosomes, not least because the former is far more abundant than the latter, meaning a better chance of recovering it.

The results, published in the journal Plos One, reveal the level of diversity in the mitochondrial DNA among the ancient birds is similar to that among modern wild birds, and have similar levels of potentially harmful mutations. However, the team says if the ancient Egyptians farmed sacred ibises, the genetic diversity in ancient birds would probably be lower due to high levels of inbreeding.

Wasef said this suggested that, rather than being bred in a mass-farming situation, sacred ibises were tempted to local areas and kept in a natural habitat – or perhaps captured and kept in farms for a short time, ready for sacrifice.

“[The most likely thing is ] next to each temple there was like a lake or a wetland – it is a natural habitat for the ibis to live in and if you are giving them food they will keep coming,” she said. Indeed, she notes, there was a swamp near Tuna el-Gebel and the Lake of the Pharaoh near Saqqara.

Pontus Skoglund, an expert in ancient DNA at Francis Crick Institute said the work was impressive. “It is really cool to get this 2,500-year-old ancient DNA,” he said.

But while he said the data backed up the possibility the sacred ibis mummies were indeed wild birds, he noted that if a hatchery was large enough the birds within might have retained a high level of genetic diversity.

“If there was a hatchery and even occasionally wild birds would breed with the birds in the hatchery, that is a second possibility,” he said.

However, Wasef said no hatchery structures had been found. What’s more, in a votive offering, a bird was killed, dipped in hot resin and wrapped to form the mummy – yet some of the mummies found in the catacombs are fake, being little more than a feather, nest material or piece of eggshell. Wasef says if birds were readily available from hatcheries, there would be no need for fake mummies.

Prof Paul Nicholson, an expert on Egyptian animal cults from Cardiff University, said a small number of ibises worshipped in temples as divine incarnations of Thoth might have been bred by humans, but that the team’s conclusions about the birds for votive offerings hold water.

“I think the idea that you might encourage flocks to stay longer or come to a particular area at particular times of the year is a good one,” he said.

• This article was amended on 14 November 2019 because an earlier version said the genetic diversity in ancient birds would probably be lower due to high levels of interbreeding. This has been corrected to say due to high levels of inbreeding.