Cooperation with neighbours may be crucial for the persistence of populations in stressful environments. Yet, cooperation is often not evolutionarily stable, since noncooperative individuals can reap the benefits of cooperation without having to pay the costs associated with cooperation. Here we show that active aggregation leading to self-organized spatial pattern formation can promote the evolution of cooperativeness. To this end, we study the effect of movement strategies on the evolution of cooperation in mussel beds. Mussels cooperate by attaching themselves to neighbours via byssal threads, thereby providing mutual protection. Using an individual-based model for mussel bed formation, we first demonstrate that the spatial pattern and the corresponding number of neighbours strongly depends on the movement strategies of the mussels. With an evolutionary model, we then show that this has important implications for the evolution of cooperation, since the evolved level of cooperativeness (the number of byssus threads produced) strongly depends on the number of neighbours and on the harshness and variability of the environment. Our results suggest that spatial aggregation, abundantly found in self-organized ecosystems, may promote the evolution of cooperation.