A thorough study of the structural, electronic, and hydrogen-desorption properties of β- and γ-MgH2 phases substituted by selected transition metals (TMs) is performed through first-principles calculations based on density functional theory (DFT). The TMs considered herein include Sc, V, Fe, Co, Ni, Cu, Y, Zr, and Nb, which substitute for Mg at a doping concentration of 3.125% in both the hydrides. This insertion of TMs causes a variation in the cell volumes of β- and γ-MgH2. The majority of the TM dopants decrease the lattice constants, with Ni resulting in the largest reduction. From the formation-energy calculations, it is predicted that except for Cu and Ni, the mixing of all the selected TM dopants with the MgH2 phases is exothermic. The selected TMs also influence the stability of both β- and γ-MgH2 and cause destabilization by weakening the Mg-H bonds. Our results show that doping with certain TMs can facilitate desorption of hydrogen from β- and γ-MgH2 at much lower temperatures than from their pure forms. The hydrogen adsorption strengths are also studied by density-of-states analysis.