Using hybrid functional calculations, we study the diffusion and thermal stability of hydrogen in Mg-doped GaN. Compared with the generalized gradient approximation, we obtain a higher activation barrier for dissociating a Mg-H complex, which is attributed to the increase in the binding energy of Mg-H. Kinetic Monte Carlo simulations yield the annealing temperature of around 800 degrees C for activating Mg acceptors, close to the measured values. The results provide an insight to understanding the annealing effect such that the annealing temperature generally increases with the Mg-H concentration, and the retrapping of H is partly responsible for the low doping efficiencies at high Mg concentrations. (C) 2012 The Japan Society of Applied Physics