First off, magnesium is not more common than calcium where it counts for Earth organisms, namely the crust. By mass calcium is more common in the crust, by atoms they are about equal (the enrichment of calcium in Earth's crust is described here). Plus, at least some organisms might prefer to save their magnesium for chlorophyll, which is rather important for biology in Earth in its own right.
Calcium and magnesium are both alkaline earth metals, but their compounds have subtly different properties. Solubility, which us a major consideration for forming stable and robust skeletal structures, is among them. Among carbonates, calcium carbonate has a solubility of $0.013\text{ g/L}$ at $25°\text{C}$; the corresponding figure for magnesium carbonate is $0.139\text{ g/L}$. When magnesium carbonate is dissolved in water, much of it is in the form of ion pairs which favors greater solubility and also makes that solubility greater than that predicted from the usual solubility product. Calcium ions, being bulkier, are less effective at forming ion pairs with multiple charged anions. Clearly calcium carbonate is more likely to precipitate from ocean water to form shells, pearls, etc. Even where there is magnesium carbonate, it is often in the form of a double salt with the less soluble calcium carbonate component (dolomite).
Phosphates tell a similar story. Wikipedia does not give quantitative data for the solubility if $\ce{Mg3(PO4)2}$ or $\ce{Ca3(PO4)2}$, but the latter has a lower solubility product ($2×10^{-29}\text{(Ca)}<1×10^{-25}\text{(Mg)}$) and again, we would expect ion pairing with a multiple charged anion to favor magnesium phosphate dissolution even more. So as with carbonates, calcium phosphate is easier to precipitate and more resistant to (re)dissolution than the magnesium counterpart.
biochemistrytag. – Fred Jan 11 '24 at 05:58