Essentially, religion is a code of conduct for a civil society. It all started from here. With society’s progression, the code of conduct also evolved resulting in its expansion and formalization.
As civilization started taking roots management of the society began.
Origin of religion can be traced as part of human evolution. Ancient religious orders basically were a set of laws and principles for some acceptable behavior in an emerging civil society.
Later all aspects of human cultures, including presumptions and myths, and overwhelming elements of nature were covered in one order. In all these developments social unity and coherence were the natural needs and dependencies of an advancing society.
An organized collection of beliefs and behaviors, a set of ideas started pouring in this social construction. And with the introduction of the Supreme Being, man's most intuitive conception or imagery, the assemblage got sanctified.
(Excerpts from Hinduism Beyond Rituals, Customs, And Traditions)