We ask why earthquakes happen, and it turns out tectonic activity is neccessary to shield the Earth from cosmic rays.
DMT, taken on it's own or in ayahuasca, occurs naturally in the body, during birth and death, it may help avoid panic or some other functions. Cannabinoid compounds are found in breast milk. Opiod receptors are involved in highlighting relevant pain. Amphetamine is an adrenalin analogue.
Ketamine seems to knock out parts of the brain, causing other parts to 'step in'. Brain areas involved in the sense of self, and in making sense of our visual field go offline, and depersonalised suggestiveness can allow dream-like processing of concerns, perhaps including breaking out of habits.
Many mushrooms and cacti have active compounds. LSD is found in a fungi on one of the worlds most common foodstuffs, but with other poisonous compounds preventing any positive use from that source - hard to imagine what a deity was thinking there. Sadly not much is known about what systems these compounds activate or interfere with, because the research got cancelled after being brought into disrepute half a century ago. Possibly something like the DMT system, involved with consciousness formation or reformation in extremis.
Alcohol, the only mind/mood altering substance unambiguously mentioned in the bible, is a source of calories, and a general toxin - the only substance that damages every cell in your body (oxidation). We think we inherited tolerance to it from small lemur-like ancestors who consumed fermented flower nectars and fruit. Alcohol has a handy affect in humans, of while judgement is impaired, it also raises testosterone in women increasing libido, and reduces it (on average) in men lowering aggression. That gives a model for how a biological need for calories, with a side effect of intoxication, could develop to have a social role, especially in courtship (honeymoon - from mead dowry gift).
We have been developing, socially, culturally, even during biblical times. Distilling spirits, for instance. Mind altering compounds show great promise in treating addictions and mood disorders. Old-style cannabis resins (but not new style 'skunk' types) turn out to have powerful anti-psychotic compounds, as well as euphoric ones. People may have been widely self-medicating since ancient times. Cannabis is an unusually safe medication during childbirth, and is the oldest recognised medicine for that going back to the late stone age (an ice body). As well as allowing a kind of 'recalibration' of reward and impulse, there are also proposed mechanisms of aiding creativity, through stimulating synaesthesia and accompanying metaphorical and abstract thinking, and through interfering with mental routines. It has been widely proposed that the role of shaman found in almost all ancient societies, may have used such states to develop solutions to social problems eg. https://aeon.co/ideas/whence-comes-nihilism-the-uncanniest-of-all-guests
Many cross-cultural religious practices, like fasting, meditation/prayer, living as a hermit, chanting, and creation of sacred art, seem to be aimed at a similar nexus of goals. Stepping outside of normal life and thinking, to seek answers to 'deeper' or non-ordinary problems. We could argue that both priests and philosophers are modern manifestations of the shaman role.