Despite the widespread observations on the osteogenic effects of magnesium ion (Mg2+), the diverse roles of Mg2+ during bone healing have not been systematically dissected. Here, we reveal a previously unknown, biphasic mode of action of Mg2+ in bone repair. During the early inflammation phase, Mg2+ contributes to an upregulated expression of transient receptor potential cation channel member 7 (TRPM7), and a TRPM7-dependent influx of Mg2+ in the monocyte-macrophage lineage, resulting in the cleavage and nuclear accumulation of TRPM7-cleaved kinase fragments (M7CKs). This then triggers the phosphorylation of Histone H3 at serine 10, in a TRPM7-dependent manner at the promoters of inflammatory cytokines, leading to the formation of a pro-osteogenic immune microenvironment. In the later remodeling phase, however, the continued exposure of Mg2+ not only lead to the over-activation of NF-κB signaling in macrophages and increased number of osteoclastic-like cells but also decelerates bone maturation through the suppression of hydroxyapatite precipitation. Thus, the negative effects of Mg2+ on osteogenesis can override the initial pro-osteogenic benefits of Mg2+. Taken together, this study establishes a paradigm shift in the understanding of the diverse and multifaceted roles of Mg2+ in bone healing.