Explaining cooperation in groups remains a key problem because reciprocity breaks down between more than two. Punishing individuals who contribute little provides a potential answer but changes the dilemma to why pay the costs of punishing which, like cooperation itself, provides a public good. Nevertheless, people are observed to punish others in behavioural economic games, posing a problem for existing theory which highlights the difficulty in explaining the spread and persistence of punishment. Here, I consider the apparent mismatch between theory and evidence and show by means of instructive analysis and simulation how much of the experimental evidence for punishment comes from scenarios in which punishers may expect to obtain a net benefit from punishing free-riders. In repeated games within groups, punishment works by imposing costs on defectors so that it pays them to switch to cooperating. Both punishers and non-punishers then benefit from the resulting increase in cooperation, hence investing in punishment can constitute a social dilemma. However, I show the conditions in which the benefits of increased cooperation are so great that they more than offset the costs of punishing, thereby removing the temptation to free-ride on others' investments and making punishment explicable in terms of direct self-interest. Crucially, this is because of the leveraging effect imposed in typical studies whereby people can pay a small cost to inflict a heavy loss on a punished individual. In contrast to previous models suggesting punishment is disadvantaged when rare, I show it can invade until it comes into a producer-scrounger equilibrium with non-punishers. I conclude that adding punishment to an iterated public goods game can solve the problem of achieving cooperation by removing the social dilemma.