To understand the origins of religion I think we have to place ourselves at the dawn of human consciousness; in that transition from animal mentality to human mentality. It would have been during this period that pre-rational associations between events and their circumstances would have formed. The early humans would have been motivated to try and make favourable events more likely and unfavourable events less likely. For example, a place where the death of a child occurred would become associated with the feelings aroused by the calamity; it would evoke memories and their associated feelings; it would take on an atmosphere or spirit. Such a "spirit" or atmosphere might become personified as a genus loci or demi-god; a fairy perhaps.
Once the sense of a spirit had taken hold, the door is open for all the behaviours that we associate with belief in a spirit to take hold as well: the desire to remain in a positive relationship with the spirit; the desire to maintain the right attitude toward the spirit; the repetition of behaviour to propitiate the spirit and encourage favourable outcomes. This surely is the origin of the religious urge, the religious sense?
Let's look a little more closely at the repetitious behaviour that the religious sense induces. Offerings may be made to the spirit in order to try and gain favour, to persuade the spirit to protect the supplicant from harm. If the person has a run of good luck following these rituals, good outcomes are associated with the performance of the rituals. The spirit is seen to be pleased by them. Behavioural psychology has shown that this will encourage the repetition of the rituals. Also, when the rituals are partially rewarded with good outcomes it has a greater effect of reinforcing the ritualistic behaviour than if the reward appears to occur consistently. Moreover, the effect is even greater when the partial reinforcement occurs randomly.
What we see here is a scenario in which events, both personal behavioural events and external events, come to be interpreted as having some relationship to each other, as being in a relation of mutual influence. It doesn't matter if the events have no causal connection, they are perceived as having one. This is the foundation of ritual and superstition. Superstition is the belief that certain behaviour will bring good or bad luck (like walking under ladders). If one succumbs to superstitious belief it can be very difficult to shake off. The risk of not performing the required behaviour or acting in a "forbidden" manner can arouse a lot of anxiety which will triumph over the attempt to escape.
Once the sense of a spiritual presence has been established in the minds of a people the foundation is laid for all the manifestations of religious experience from the most basic superstitious forms to the most complex theologies. People interpret experiences as being guided by "the spirit"; events are treated as messages, as rewards, as punishments. Those deemed to be disrespectful of the spirit may have their misfortunes interpreted as retribution from the spirit; benign experiences will be seen as rewards for adopting the correct attitude or carrying out the correct behaviour. All this experience and thinking combines to reinforce yet more the believer's belief. But it's all entirely circular.
Belief is also reinforced by the special activities associated with the spirit. Mass communal events strengthen the sense of uplifting unity among believers; music played especially for worshipping the spirit adds to the psychological thrill; those appointed to speak on behalf of the spirit are imbued with huge authority and charisma. The spell, the magic increases in complexity and enchantment until a huge edifice of spiritual authority is erected and stands as a physical manifestation of the spirit. All this has been built on those primordial associations of events, affects, and repeated behaviours handed down through the ages as something real, vital for survival, and as a guarantor of good prospects after death.