Since you ask, religion is a by-product of the way the human mind has evolved. It boils down to 2 or 3 aspects of the way human mind's work. I'll start with the conscience. Conscience is a sense of being watched. This is an evolved check on our behaviour that evolved alongside language. (not too hard to work out why when you remember that passing on your genes is the goal and social reputation and status have a big influence on this.) Self consciousness goes along with it too and is the result of the fact that, with language, other people can talk about you. ("Oh no, everyone will know!" is the worry when you are found out.) This sense of being watched is the basis of what religious people mean when they say they have a relationship with god. They really mean they are having an elaborate conversation with their conscience.
Secondly there is the sense that our consciousness won't end. It isn't too hard to see why. Imagining one's own extinction isn't going to be a great evolutionary driving force. As we can't imagine a world without us, we instead imagine the world carrying on but with us watching. This all adds up to the beginning of feelings that our consciousness carries on after death, despite all evidence to the contrary.
Third there is the human habit of assigning inanimate objects and events with human causes and personalities (sun gods, gods of rivers, harvests etc, you name it), which is an overflow of our ability to see other humans as 'minds' - which is necessary for socialising in a community using language. Modern children (should) grow out of this habit with education. Primitive tribesman still have the same gods wherever you go in the world.
Add basic animal feelings and emotions into the mix (especially the emotions involved in pair bonding and reproduction) and you have one very confused primitive man. And, hey presto: religion. That's a very cursory potted version of where it came from. There's more, but those are the basic points that occur to me now.
In summary: Religion is a by-product of the evolution of the human mind.