In ab initio theory, defects are routinely modeled by supercells with periodic boundary conditions. Unfortunately, the supercell approximation introduces artificial interactions between charged defects. Despite numerous attempts, a general scheme to correct for these is not yet available. We propose a new and computationally efficient method that overcomes limitations of previous schemes and is based on a rigorous analysis of electrostatics in dielectric media. Its reliability and rapid convergence with respect to cell size is demonstrated for charged vacancies in diamond and GaAs.