The history of human societies is consistent with an assertion that religion is necessary for long-term human survival.
Without a religion, humans go crazy (become incoherent about matters of basic common sense - look around you...) and they despair; strategically destroying their own societies; and on-average they grossly fail to reproduce even at the minimum replacement level.
Of course some religions 'work' better than others in any particular situation, they out-compete the others; but I want to focus on the fact that religion-as-such is necessary, and to ask: Where does religion come from?
A true but partial answer is that a religious perspective is built-into all healthy humans, and is present at least in young childhood - even if it is later suppressed or lost. In other words, religion is innate; and atheism and materialism are acquired.
But why is religion innate? Why are we all born into the world with a spontaneously religious attitude - believing in all sorts of immaterial things like the soul, gods, life beyond death, the aliveness and consciousness of (what adults term) 'inanimate' objects etc. ?
One answer is that religion is a fortunate accident.
It 'just happened', for no purposive reason (maybe some undirected mutations that just happened to happen...); but religion is found in humans because if it were not found, then there would be no humans.
(As a negative thought experiment: those ancient humans that inherited an immunity to religion, did not leave behind as many offspring, so their genes became extinct - or something on those lines...)
This is a non-explanation, masquerading as an explanation; but some modern people are happy for things to be non-explained that way - and if so then that is an end of discussion.
Or else (if we actual require a real explanation) religion was Put There.
But what-by? Some kind of deity must be the answer; even if that deity is just 'the way things are in this universe'. Or it may be put-there by a personal god or gods.
Religion may be found because it is 'in-born' or because it is externally-communicated - or both.
If it is inborn; then we know about religion by knowing what is within us (by introspection, or intuition); although we will surely make mistakes about this knowledge due to our limited capacity and various biases (accounting for the variety of religions around the world and through time).
(And - for Christians - we all know inwardly about religion because we are children of God; we know about deity because there is deity within each of us.)
And/ or we may have religion communicated to us by revelation (by being told by the deity, one way or another) - or some variant of revelation, such as divination. Again the constraints will apply - we can only know as limited by our capacity and via our personal biases.
That seems to be the situation: everybody normal (non-pathological) is born with religion built-in by some combination of, or selection from, inborn-internal and revealed-external sources. Such 'generic' religion is necessary for the sustainable biological and psychological functioning of humans.