This gets a little complicated with the fact that many exogenous psychoactive drugs work because they mimic endogenous chemicals - we have receptors for cannabinoids and opioids because we
produce cannabinoids and opioids. The effect of the exogenous versions isn't so much an accident as that some plants produce substances that bind to the same receptors, and we're smart enough to have figured that out and learned to cultivate them.
The argument could be made that this serves some vast evolutionary purpose, but then we're back to inventing gods for ourselves again. (And in any case, such a theory would need also to explain why wasps also enjoy getting drunk, this being after all another example of the same behavior. Evo-psych types being as anthropocentric as they are, this never seems to be addressed, but it needs to be if they want the intellectual coherence to which they so anxiously pretend.)