Hydrogen, carbon, and oxygen are common unintentional impurities of Al (x) Ga (1−x) N crystals. This impurity structure and its interplay with Mg impurities in Al (x) Ga (1−x) N semiconductors are relevant to develop the p-type nitride crystals for various devices (e.g, LEDs, transistors, gas sensors) but are still unclear. Here we have investigated Mg-doped Al 0.5 Ga 0.5 N before and after postgrowth annealing with valence-band and core-level photoelectron spectroscopy, photoluminescence, and resistivity measurements. First, it is found that a surface part of the Al 0.5 Ga 0.5 N crystal is surprisingly inert with air and stable against air exposure-induced changes. Thus, the relatively surface-sensitive photoelectron spectroscopy measurements reflect in this case also the bulk crystal characteristics. The measurements reveal the presence of deep states up to 1 eV above valence-band maximum before and after the annealing and that oxygen and carbon occupy N lattice sites (i.e., O N and C N ). The model where C N -induced acceptor states in the band gap participate in the blue emission (photoluminescence) is supported. Furthermore, the presented Mg 2p core-level spectra demonstrate that part of Mg atoms forms direct bond(s) with oxygen in the bulklike structure of Al 0.5 Ga 0.5 N and that the chemical environment of Mg atoms is much richer than was expected previously.