Magnesium (Mg) homeostasis is critical for metabolism. However, the genetic determinants of the renal handling of Mg, which is crucial for Mg homeostasis, and the potential influence on metabolic traits in the general population are unknown. We obtained plasma and urine parameters from 9099 individuals from seven cohorts, and conducted a genome-wide meta-analysis of Mg homeostasis. We identified two loci associated with urinary magnesium (uMg), rs3824347 (4.4×10) near , which encodes an epithelial Mg channel, and rs35929 (2.1×10), a variant of , which encodes a GTP-binding protein. Together, these loci account for 2.3% of the variation in 24-hour uMg excretion. In human kidney cells, ARL15 regulated TRPM6-mediated currents. In zebrafish, dietary Mg regulated the expression of the highly conserved ortholog, and knockdown resulted in renal Mg wasting and metabolic disturbances. Finally, rs35929 modified the association of uMg with fasting insulin and fat mass in a general population. In conclusion, this combined observational and experimental approach uncovered a gene-environment interaction linking Mg deficiency to insulin resistance and obesity.