The idea of God was invented around the same time civilization was. Civilization could not have been built without slavery. You had two ways of supporting yourself as a prehistoric human, gathering your own food or stealing it from someone else. Civilization happened when someone had the bright idea of forcing other people to grow it for them, and then taking it and using it to fund your army.
You have to have had a certain amount of megalomania to have the stones to basically enslave another people. And the people who you relied on to actually do the enslaving needed a legitimate reason to go do the enslaving, which is a mean and nasty thing to do to people. Religion provided that justification, and it didn’t take long before the people in charge had the bright idea to co-opt these religions for political gain.
The book Fields of Blood provides an account of early religions. Karen Armstrong is an award-winning scholar with both religious and academic bonafides.