In a controlled environment, the goal is to be sterile. Obviously in the wild mushrooms are exposed to a variety of harmful/negative stimuli (animals, weather, humans who hunt them, infection/disease etc.)
So to answer your question, they don't necessarily avoid all contamination in nature. The more careful and clean/sterile you are with an indoor grow, the higher your yields and success rate will be.
There are many ways to grow a mushroom but if you are going with a tek that uses jars it is "wise to sterilize." There may be other ways to do it without a pressure cooker but I have not looked into such methods. In my experience, it is nice to have and mine only cost me about $65 or so a few years ago and it still works great!