Allen Buchanan and Russell Powell look at it from an evolutionary standpoint:
Morality involves judgment, shame and exclusion as much as kindness. What is more, modelling work by the anthropologist Robert Boyd, the biologist Peter Richerson, the economist Samuel Bowles and their collaborators has shown that moralising punishment is likely to evolve only in the context of selection between groups. Which means that morality most likely evolved in an arena of intergroup conflict, in which violence and vigorous economic competition between groups was commonplace.
This conclusion is consistent with archaeological, ethnographic and ethological data, too. As the archeologist Lawrence Keeley, the psychologist Steven Pinker, the anthropologist Chris Boehm, the primatologist Richard Wrangham and others have observed, intergroup conflict is common in extant and prehistoric hunter-gatherer bands, and is well established in chimpanzees.
Given this picture, we should expect human beings to extend moral consideration to members of their own group, while at the same time ruthlessly exploiting people from other groups. In short, we should expect evolution to have produced a human moral psychology that is group-ish and strategic in nature — one that takes other individuals to be part of the moral community if they are part of one’s co-operative group, or otherwise capable of contributing to or disrupting co-operative goods. Extending moral consideration to outsiders — especially those who are not in a position to reciprocate or who could be exploited without fear of reprisal — is maladaptive in a moral system that arose from competition between groups. In other words, a conventional evolutionary view is that morality involved as a way of bolstering in-groups and excluding others – that we are ‘hard-wired’ for tribal loyalties and conflicts.