The way psilocybin goes to work on the brain of a fly could provide an explanation for why several different species of mushroom all evolved to contain the exact same compound.
Although it causes hallucinations in humans, psilocybin could be tricking insects into losing their appetite - making it a protective mechanism against being eaten by an hungry invertebrate.
Psilocybin isn't limited just to one type, or even one genus, of mushroom - the psychedelic compound appears in over 200 shroom species
That may not seem like many compared to an estimated 5.1 million fungus species in the world, but what's curious is that it appears in wildly varying species that otherwise don't seem to have much in common at all.
This, according to researchers at the Ohio State University, looks like something called horizontal gene transfer. This is when genetic material is transferred between organisms by means other than hereditary transfer - for instance, by way of a virus.
It often occurs in response to a stressor. Antibiotic resistance among bacteria is a good example of the horizontal transfer of genetic material via bacterial conjugation in response to a threat.
Led by evolutionary fungal genomicist Jason Slot from the Ohio State University, a team of researchers has studied a range of both psilocybin and non-psilocybin mushrooms, and narrowed the similarity in psilocybin mushrooms down to a cluster of five genes.
Between them, these genes produce the enzymes needed to create psilocybin - and they seem to have transferred from species to species as a group.
"But our main question is, 'How did it evolve?'" Slot said. "What is the role of psilocybin in nature?"
To answer that question, the team had to look at what psilocybin does.
In humans, it causes hallucinations by suppressing a particular neurotransmitter. But in insects, suppression of this neurotransmitter has a different effect - it dampens appetite.
This makes a difference because the horizontal gene transfer of the gene cluster noted by the team seems to have occurred in environments with lots of insects, such as animal manure and rotten wood.
"We speculate that mushrooms evolved to be hallucinogenic because it lowered the chances of the fungi getting eaten by insects," Slot said.
"The psilocybin probably doesn't just poison predators or taste bad. These mushrooms are altering the insects' 'mind' - if they have minds - to meet their own needs."
How exactly the gene cluster transferred is a mystery, since horizontal gene transfer across mushrooms isn't common.
But its presence in such varying species - even lichen has it - that have closer relatives without psilocybin is a strong indicator that it has indeed transferred.
In the non-animal world, plants are well-known for the development of chemical compounds as a defence mechanism. The camphor tree, for instance, produces an insect-repellent substance called camphor that can be used to create mothballs, while the geranium produces a chemical in its flowers that paralyses beetles.
Capsaicin, produced by many plants in the capsicum family, even evolved specifically to deter mammals (humans are clearly not good at taking heed of nature's warnings), but has no effect on birds, who can therefore disperse the plants' seeds.
But identifying the genes responsible for the production of psilocybin could be of great benefit to medical research, which has for some time been looking into the therapeutic benefits of the compound to treat mental illness.
The team's research has been published in the journal Evolution Letters.