The electronic and optical properties of hexagonal wurtzite AlN doped with Zn and Mg are studied based on the density functional theory. The density of states, dielectric function and absorption spectra are calculated using plane-wave ultrasoft pseudo-potential and the generalized gradient approximation (GGA). The absorption peaks are found during 0-13 eV and 43-48 eV in Mg doped AlN, while in Zn doped AlN, only during 0-15eV. The absorption peak about 43-48 eV found in Mg doped AlN is due to the transition of the deep 2p energy level in Mg to conduction band. The first peak of the dielectric imaginary part is related to the transition of the doped atoms. The peak of the dielectric imaginary part and absorption peak appears about 8 eV is due to the transition of N 2p to Al 3p state. Results show that the electronic and the optical properties of hexagonal wurtzite AlN are directly related to the electronic structure of the impurities in the crystal.