Temperate bacteriophages integrate in bacterial genomes as prophages and represent an important source of genetic variation for bacterial evolution, frequently transmitting fitness-augmenting genes such as toxins responsible for virulence of major pathogens. However, only a fraction of bacteriophage infections are lysogenic and lead to prophage acquisition, whereas the majority are lytic and kill the infected bacteria. Unless able to discriminate lytic from lysogenic infections, mechanisms of immunity to bacteriophages are expected to act as a double-edged sword and increase the odds of survival at the cost of depriving bacteria of potentially beneficial prophages. We show that although restriction–modification systems as mechanisms of innate immunity prevent both lytic and lysogenic infections indiscriminately in individual bacteria, they increase the number of prophage-acquiring individuals at the population level. We find that this counterintuitive result is a consequence of phage–host population dynamics, in which restriction–modification systems delay infection onset until bacteria reach densities at which the probability of lysogeny increases. These results underscore the importance of population-level dynamics as a key factor modulating costs and benefits of immunity to temperate bacteriophages.