We present the results of first-principles theoretical calculations for the N compensation mechanism in ZnO. We find that N acceptors are mainly compensated by O vacancies, N-2 molecules at anion sites. and N-acceptor-Zn-antisite complexes, which limit p-type doping. In ZnO codoped with N acceptors and Ga donors, the acceptor level of a N-Ga-N complex is similar to that for an isolated N acceptor, and hole concentrations are not enhanced due to the compensation by Ga donors.