The great Harvard Professor of Psychology, B.F.Skinner showed how pigeons become religious when given random rewards that have nothing to do with their behaviour.
This was a spin-off from the experiments on 'operant conditioning' where he showed how pigeons can be trained to carry out an action by associating it with a reward. For example, pigeons can be trained to peck a green light if they get a pellet of food every time they do so. Skinner wondered what would happen if the reward was completely dissociated from their actions.
One of Skinner's experiments examined the formation of superstition in one of his favorite experimental animals, the pigeon. Skinner placed a series of hungry pigeons in a cage attached to an automatic mechanism that delivered food to the pigeon "at regular intervals with no reference whatsoever to the bird's behavior." He discovered that the pigeons associated the delivery of the food with whatever chance actions they had been performing as it was delivered, and that they subsequently continued to perform these same actions.So, random, ritual movements become associated in the pigeon brain with rewards when the two are, in reality, dissociated.
One bird was conditioned to turn counter-clockwise about the cage, making two or three turns between reinforcements. Another repeatedly thrust its head into one of the upper corners of the cage. A third developed a 'tossing' response, as if placing its head beneath an invisible bar and lifting it repeatedly. Two birds developed a pendulum motion of the head and body, in which the head was extended forward and swung from right to left with a sharp movement followed by a somewhat slower return.Skinner suggested that the pigeons behaved as if they were influencing the automatic mechanism with their "rituals" and that this experiment shed light on human behavior:
The experiment might be said to demonstrate a sort of superstition. The bird behaves as if there were a causal relation between its behavior and the presentation of food, although such a relation is lacking. There are many analogies in human behavior. Rituals for changing one's fortune at cards are good examples. A few accidental connections between a ritual and favorable consequences suffice to set up and maintain the behavior in spite of many unreinforced instances. The bowler who has released a ball down the alley but continues to behave as if she were controlling it by twisting and turning her arm and shoulder is another case in point. These behaviors have, of course, no real effect upon one's luck or upon a ball half way down an alley, just as in the present case the food would appear as often if the pigeon did nothing—or, more strictly speaking, did something else.
Source: Wikipedia - B.F.Skinner
What has this got to do with religion?
Religious people believe that prayers and rituals will bring about rewards in the form of something favourable or desirable happening or something undesirable not happening. Examples of these random movements and rituals include:
Religious people are, in fact, behaving exactly like Skinner's pigeons and performing essentially random actions which have subsequently been ritualized in return for 'rewards' which are themselves merely imaginary or random and unrelated to their actions. The difference between religious people and pigeons is that religious people have learned these random actions from their culture whereas pigeons devise their own anew.
No doubt, if Skinner's pigeons could talk and rationalise their behaviour they would use the same arguments that religious people use in defence of their rituals; that when they don't provide a reward it's because they haven't performed the ritual correctly or the reward was being withheld because they didn't deserve it, and when they appeared to work, that proved the ritual works. Either way it reinforces the bizarre behaviour and makes it more important for them to carry it out, not less.
Further reading: Psychologistworld.com - Superstition