“Deterring oneself from the pursuit of self-interest because of the risk of punishment from a watchful supernatural eye would seem to reduce an individual’s evolutionary fitness, and should thus be eliminated by natural selection,” Johnson wrote. “However, even if such beliefs are false and costly, they may have generated net benefits: to individuals, by steering them away from selfish behavior that risked retaliation in increasingly transparent and gossiping human societies; and/or to groups, by increasing the performance of the group as a whole in competition with other groups.”
This notion of how cooperation among strangers emerges is, of course, an old and intriguing question. As in many cases, it's helpful to turn to the old Scottish moral philosopher for some anticipation of these results. Adam Smith (the author of much more than the narrow "invisible hand" idea for which he is mostly known - see Gavin Kennedy's website for lots on this) wrote some fascinating things about moral rules, as described in this article from the Stanford Encyclopedia of Philosophy:
"[Smith] also says that it adds to the sacredness we attribute to moral rules to see them as laws of the Deity, and to the importance of morality as a whole to see it as a way of “co-operat[ing] with the Deity” in the governance of the universe (166). And he shows how a belief in an afterlife may be necessary if we are to see the universe as just, which in turn is important if we are to maintain our commitment to the value of acting morally (168–70). In all these ways, but especially the last, he anticipates Kant's moral argument for belief in God, without ever quite insisting that there is a God. At the same time, he makes clear that any religion that gives priority to ritual or creed over morality is baleful, and poses one of the greatest dangers to a decent and peaceful society (TMS 176–7; cf. WN 802–3)."