Incorporation of impurities in CuAlO2 provides an opportunities to modulate its electronic and optical properties, which can be exploited for the applications of optoelectronic devices. Among the various elements doped of CuAlO2, research on the codoping magnesium (Mg) with nitrogen (N) which may be a promising way for fabricating p-type CuAlO2 is still limited. Here, the first-principles calculation based on density functional theory was used to investigate the electronic and optical properties of Mg-doped, N-doped and Mg–N codoped CuAlO2. Compared with the undoped CuAlO2, the lattice parameters a and c of the Mg–N codoped CuAlO2 become larger and smaller, respectively. The acceptor level induced by 2p state of N in N-doped CuAlO2 is very deep. The undoped and Mg-doped CuAlO2 has indirect band gap. Whether the deep acceptor level or the indirect band gap, it is unfavorable to p-type doping or light emission. Due to the hybridization of 3p state of Mg and 2p state of N on the top of the valence band, the Mg–N codoped CuAlO2 not only has a direct band gap, but also has a shallower acceptor level (about 0.24 eV above the top of the valence band). The optical properties of CuAlO2 changes obviously after Mg–N codoping in the area of low energy. These results suggest that the possibility of enhancing the hole concentration in CuAlO2 by Mg–N dual-acceptor codoping which will be beneficial to the application of CuAlO2 in optoelectronic devices.