You could have some kind of subterranean bacteria that lives in the mines and your psychoactive chemical could be a secretion of this biological goo as it eats at the ore that is mined here.
Medieval peoples didn't really know what a bacteria was, so it could just seem to them to be some kind of slime that grows on exposed ore, or it could bioluminescence on the mineshaft walls. This would make it seem magical or fit in with your religious requirement. The only real sticky point here is that even though you know it's bacteria for someone of that era they would not necessarily know that. So I would be careful on how I describe this within their worldview as they may not understand how it works on a fundamental level. For them it could be just "soul fire" or some other quasi mystic stuff, like "faerie dust".
I wouldn't be a stretch to say they found it originally in a natural tunnel, that was used by these previous peoples as a place of worship. It could be a naturally occurring bacteria.
This stuff could be easily killed by UV radiation so that it's hard to transport the live bacteria (which is harmless / non-infectious if ingested) the point is to control where it can be produced.
If it's just a plant growing on the side of the mountain, it would be easy to cultivate and "steal" it from them. But if it only grows on a particular type of ore and needs care when transporting it to "seed" a new mineshaft it's much harder for someone to do that. This way they could maintain a monopoly on it. This would make the environment/area it grows in more valuable.
For example: Maybe it would have to be shielded from the harsh light when transported. Perhaps it could be transported only short distances or even require an environment that matches the original tunnels it came from while being transported. Which may not be an easy feat with their tech level, but could allow them to scale up production by transplanting it to new mine shafts locally.
There could be some special ways to collect and process the stuff it produces, but I cant think of anything else that's creative in that regard.
Just an idea I didn't see posted