It was actually the other way, religion was the ideological superstructure for the first class societies and state forming. Originally, as observed in the ancient Egypt and Mesopotamia religion gain power as communities were formed around common work 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.
It was actually the other way, religion was the ideological superstructure for the first class societies and state forming. Originally, as observed in the ancient Egypt and Mesopotamia religion gain power as communities were formed around common work 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.