The mechanisms giving rise to religion are hardwired into our brain. This is hard to argue against, if you accept a monist interpretation of the brain. I would like to point out two anecdotes that would support this and point out some possible mechanisms accounting for the effects we see.
My cat whines every night outside my door. He wants me to let him in to sleep with me. He knows that if he whines at a door, eventually I will come open it for him. I have given in to his annoying meows enough for him to learn that eventually, if he keeps meowing, I will open the door for him. This is called conditioning.
A person I know is made really nervous by the thunderstorms that constantly cruise past her house. She "rebukes" them by invoking the name of the Lord, as is customary in her religion. There have been many storms, but none has destroyed her house or killed her. However, given her fear of the storms, the destruction and death remain a salient possibility for her. Each time, though, she manages to make the storm pass on. This is called prayer.
Pause for a moment and consider how the two are interchangeable. Certainly the prayer is a conditioned response to the nervousness brought on by a storm. What is less apparent is that all of our learning is governed by these same mechanisms. The development of a narrative structure with which to view and control the outcome of events is what gives rise to prayer.
What I mean to say is that religion arises due to an attempt by some individual to explain the causal mechanisms behind the events occurring in that individual's purview. It is inevitable (meaning natural; governed by neural mechanisms) that certain narratives may carry more weight with certain individuals than others, for instance if they evoke fear associated with some stimulus. These narratives would become superstition, if proven, by widespread acceptance, to be "true" (by which I mean viable as a meme, or a transmissable idea). Such superstitions may be codified and ordered by some governing body (usually men, go figure) into what we would recognize as a religion.
This concept seemed to be understood by the infamous 20th century magician, Aleister Crowley, whose publication The Equinox carried the banner, "The Method of Science, the Aim of Religion." His goal was to utilize this powerful narrative-assigning mechanism to create a religious mythology for himself. From this framework, he was better equipped to handle life.
Recent neuroscience studies have found some surprising results with religious practices, such as meditation. Understanding and utilizing these narrative mechanisms could possibly be a way of self-regulating mood and emotions.