We describe Mg and O codoping experiments in gas-source molecular-beam epitaxy of GaN and AlGaN that produce high levels of Mg incorporation and activation. In order to obtain the highest level of Mg incorporation the surface stoichiometry was optimized by adjusting the NH3/Ga and NH 3/(Ga+Al) flux ratios. The lowest acceptor activation energy and the highest hole concentration, p=2×1018cm-3, were measured in samples of p-GaN and p-AlxGa1-xN with well-defined Mg/O ratios determined by secondary ion mass spectrometry. Measurements of the temperature dependence of diffusion current in p-n junctions formed in Al0.08Ga0.92N and GaN show acceptor activation energy of 195±10 and 145±15meV, respectively. Low activation energies are attributed to successful codoping.