I highly recommend a review article by Scott Atran and Joseph Heinrich, “The Evolution of Religion: How Cognitive By-Products, Adaptive Learning Heuristics, Ritual Displays, and Group Competition Generate Deep Commitments to Prosocial Religions.” It’s a great summary of current thinking about scientific explanations of religion. (Thanks to Konrad Talmont-Kaminski)
In sum, religion, as an interwoven complex of rituals, beliefs, and norms, plausibly arises from a combination of (1) the mnemonic power of counterintuitive representations, (2) our evolved willingness to put faith on culturally acquired beliefs rooted in the commitment-inducing power of devotions and rituals, and (3) the selective effect on particular cultural complexes created by competition among societies and institutions. None of these evolved for religion per se. The mnemonic power of minimally counterintuitive representations appears to be a by-product of our evolved expectations about how the world works and our fitness-enhancing requirement to pay attention to anomalies. The faith we sometimes place in culture over our own experience and intuitions is a cognitive adaptation, resulting from our long dependence on vast bodies of complex cultural knowledge. Reliance on costly displays evolved to provide partial immunity against manipulation. The power of rhythm and synchrony in ritual to build solidarity (Wiltermuth and Heath 2009) likely arises from our imitative and ToMabilities. Cultural evolution, driven by competition among groups, exploits each of these cognitive processes to fashion sets of counterintuitive beliefs, rituals, and norms that spread by intergroup transmission, conquest, or reproductive differentials. As a result, for large-scale societies, these complexes tend to include potent supernatural agents that monitor and incentivize actions that expand the sphere of cooperation, galvanize solidarity in response to external threats, deepen faith, and sustain internal harmony.