Hydroxyapatite (HAp), with the chemical formula Ca10(PO4)6(OH)2, is a bioactive ceramic material typically used in the treatment of bone defects owing to the excellent biocompatibility and the ability to bond with bone tissues. Mechanical properties of HAp have drawn pronounced attention because of the crucial roles in the medical applications. Based on density functional theory (DFT), this work reports the structural, formation energy, mechanical, Vickers hardness, electronic properties, and optical properties of hydroxyapatite (HAp), strontium-substituted HAp (Sr-HAp), and magnesium-substituted HAp (Mg-HAp). The results reveal that the Sr2+ and Mg2+ ions mainly replace the Ca2+ ion at the Ca-1 site in HAp. Resistance to hydrostatic compression followed the order Mg-HAp > HAp > Sr-HAp. Sr-HAp has the capability to enhance thermal shock resistance and lattice thermal conductivity. In addition, the electronic properties of HAp change upon substitutional Sr2+ and Mg2+ doping. In comparison with pristine HAp, the energy gap of Sr-doped HAp relatively increases, whereas that of Mg-doped HAp decreases. Nevertheless, both Sr-HAp and Mg-HAp remain insulators. Intriguingly, Sr-HAp possesses the improved hardness by 11.31 % owing to the hybridization of the Sr (4p) and O (2p) states, which contributed to its high shear modulus and high hardness. Beyond that, we further investigate their optical properties. The presence of Mg states at approximately 10 eV in the conduction band leads to enhanced absorption within the 14 – 20 eV for Mg-doped HAp. Similarly, the presence of a deep Sr(4p) level in Sr-HAp amplifies absorption within the 20–25 eV range. These findings have raised a potential avenue for selectively boosting the optical absorption of HAp, thus improving its photocatalytic properties.