Hydroxyapatite (HA) with a stoichiometry composition of Ca10(PO4)6(OH)2 is widely applied for various biomedical issues, first of all for bone defect substitution, as a catalyst, and as an adsorbent for soil and water purification. The incorporation of foreign ions changes the acid–base relation, microstructure, porosity, and other properties of the HA materials. Here, we report the results of calculations of the density functional theory and analyze the possibility of two foreign ions, CO32− and Mg2+, to be co-localized in the HA structure. The Na+ was taken into account for charge balance preservation. The analysis revealed the favorable incorporation of CO32− and Mg2+ as a complex when they interact with each other. The energy gain over the sole ion incorporation was pronounced when CO32− occupied the A position and Mg2+ was in the Ca(2) position and amounted to -0.31 eV. In the most energy-favorable complex, the distance between Mg2+ and the O atom of carbonate ion decreased compared to Mg…O distances to the surrounding phosphate or hydroxide ions, and amounted to 1.98 Å. The theoretical calculations agree well with the experimental data reported earlier. Understating the structure–properties relationship in HA materials varying in terms of composition, stoichiometry, and morphology paves the way to rational designs of efficient bio-based catalytic systems.