I like to think that, in a world before law enforcement, religion is a way clever people trick strong people into not killing them and taking all their stuff.
What is law enforcement and when has it ever not been a threat to have someone come knock your teeth in if you piss off enough or the wrong person?
I think the more likely answer is indeed in the picture: baby's first philosophy. There is a lot of wisdom and behavior grooming in it, but I'd argue the reason is the other way around: It was to try and tell leaders and fathers (in patriarchy, anyways) how to not get their teeth kicked in and how to teach and deal with bad people.
It's an instruction set that has been combined with simple history telling, and then corrupted by thousands of years of dogma and constant revision from the selfish and rich.
It's silly to anchor your moral axioms in systems that require obedience to authority or belief without evidence, and that is the only true difference between philosophy and religion.
I'm talking more than just getting your teeth knocked in. I'm talking getting murdered, your wife getting raped, and your property getting stolen. With no state protection, what is to prevent that from happening?
In the same way that the threat of future punishment by getting thrown in jail stops people from doing those things now, the threat of punishment by God or Gods would serve the same purpose.
It was actually the other way, religion was the ideological superstructure for the first class societies and states forming. Originally, as observed in the ancient Egypt and Mesopotamia religion gain power as communities were formed around common labour and consumption. The first organised cults were the fertility and agriculture gods, first temples were granaries and the priestly class started as granary managers, and that position allowed them to gather and increase their power. Especially visible in case of Mesopotamia where those origins stayed visible way into the written history period.
That is, religion was justification for the strong to become stronger and rule over weaker, and a way for rich to trick poor into not killing them and not taking stuff that was stolen back. Note that even thousands years later even nominally secular state power still had supernatural justification, divine right, mandate of heavens and so on.