This paper provides an evolutionary explanation for the different patterns of cooperation across groups in societies in which social and political identity dimensions are cross-cutting, compared to societies in which identity dimensions are overlapping. An agent-based model of interaction between individuals belonging to different groups is presented. An agent’s decision to cooperate or defect in a prisoners' dilemma game is based on the identity of the others along two identity dimensions. In the first version of the model, the two identities are equally salient, while in the second version, their relative salience varies. The results show that in the presence of cross-cutting identity dimensions, cooperation takes place not only within clusters of identical individuals, as suggested by previous models, but also along each of the two identity dimensions, between individuals that share only one of the two identity traits. As the relative salience of one identity dimension increases, cooperating along the lines of the more salient dimension becomes the most successful strategy. These findings are relevant for understanding the patterns of cooperation in heterogenous societies and under increased levels of social and political polarization.