I was reading an interesting article recently on the origins of superstition and it seems to go a long way to explaining our invention of religion.
One of the consequences of complex thought - and I don't mean Plato or Hawking - organisms from pigeons upwards to chimpanzees (and of course humans), is that an evolutionary advantage was to be had by a organism that was able to place pattern or see pattern. This ability would facilitate predictive behaviour, and hence a survival/reproductive advantage.
In the case of humans, being able to tell that the markings in the earth are the tracks of an animal that has passed not too long ago would allow us to track down a valuable source of protein. In the case of chimpanzees, being able to recognize the onset of certain digestive illnesses and notice that after ingesting an otherwise awful tasting plant the illness subsided (this amazing self-medication has been observed many times and the exact nature of it's origins are being debated). In the case of some birds, recognizing certain weather patterns etc etc.
There is an unforeseen consequence to this gift, and that consequence is to place pattern on things that are either completely random, or at least truly unpredictable. The animal will attempt to place a pattern on this unpredictable course of events when it involves one of the very important survival aspects to its life. In the case of humans, lucky rabbits feet, horeshoes, etc. You get the picture.
This superstitious behaviour has been observed in the animal kingdom as well, most notably with skinners pigeons. There was an experiment where instead of stimulus response, he simply provided the food pelet at sparse random intervals. What followed was astounding. After carefully observing the pigeons, it was noticed that all developed certain behavioural "ticks" that it would insist on doing becuase it believed that that was what it did to cause the food pellet to appear.
One pigeon kept thrusting its head towards the right corner, others would stomp a foot one time and turn around, others would stay motionless in a corner for a while and do something else.
Several paralells were discovered between the above and human behaviour.
1) It would continue despite the lack of supportive evidence.
2) Whenever the predicted event occured during the acting out of superstitious act it encouraged very much the continuing of said act. ie: If the pigeon believed that thrusting its head to the top right corner brought food pellets it would continue with a certain amount of "enthusiasm". After a random interval of time in some cases a pellet would drop and after gobbling it up the pigeon continued but this time with more fervor!
3) Countless contradictory events are ignored. In the case of humans, the fact that standing on a lucky square didn't always result in his horse winning the race was dismissed. We know because of interviews that these particular events are explained away for reasons such as "Wasn't wishing hard enough", "Didn't start on the square on time", "Lost concentration", "Wasn't meant to be, fate waiting for something else to give me" etc etc. Although we're unable to interview other organisms, this behaviour of ignoring contradictory evidence is also there.
What does this all have to do with religion?
Our thought is a great deal more complex than any other organism, so it's not such a great stretch to see that any behaviour we might have in common - we'll certainly make more complicated.
All three of the above factors are ALWAYS present in religious behaviour. Most notably in how the effects of prayer are interpreted. If it works - "See, I told you!". When it doesn't, there follows a long list of excuses, - ignoring of course the possibility that prayers aren't answered and that just like a broken clock tells the time correctly twice a day - sometimes what you do will be followed with the desired result.
So in our case, it's not surprising that something was formed out of our desire to make sense of an uncaring planet that was indifferent to our success or failure as a species. The most important factors in our life - food, sex, social behaviour, natural disasters, weather etc caused us to form rules, rituals and behaviours in an attempt to place order. With the invention or writing, it's no surprise that these rules became more permanent and codified - and hence organised.