Freshwater lens is the sole natural freshwater resource on artificial islands, and surface nitrogen infiltration is one of the primary pollutants affecting the lens. The retention process of nitrogen by coral sand directly relates to the safety and stability of the island’s groundwater environment; however, the mechanism of nitrogen fixation by coral sand in the saline groundwater environment of such islands requires further investigation. Based on a survey of the groundwater environment on an artificial island in the South China Sea, this study collected coral sand from the island and conducted static adsorption experiments under both freshwater and saline water conditions. The kinetic and thermodynamic mechanisms of ammonium nitrogen (-N) and nitrate nitrogen (-N) adsorption by coral sand were systematically analyzed, and machine learning techniques were employed to predict the inorganic nitrogen adsorption capacity in different aqueous environments. The results indicate that the adsorption processes of -N and -N by coral sand in both freshwater and saline water conform to the pseudo-first-order kinetic equation and the Langmuir adsorption model. The adsorption of -N is a non-spontaneous, exothermic, and entropy-decreasing process, whereas that of -N is non-spontaneous, endothermic, and entropy-increasing. At an initial nitrogen concentration of 100 mg/L, the adsorption capacity of coral sand for -N decreased from 0.242 mg/g in freshwater to 0.120 mg/g in saline water, and for -N from 0.140 mg/g in freshwater to 0.057 mg/g in saline water, representing reductions of over 50%. This is attributed to the competitive effects of Na⁺ and Cl⁻ ions in saline water, which inhibit nitrogen adsorption. No new phases were formed after nitrogen adsorption by coral sand. The adsorption of -N depends on physical electrostatic adsorption and cation exchange, while the adsorption of -N involves physical adsorption and the replacement of hydroxyl and acyl functional groups. The adsorption capacity for both nitrogen species increases with reaction time and decreases with increasing sand particle size and water salinity. An increase in pH inhibits -N adsorption but promotes -N adsorption, whereas temperature has the opposite effect. The Bayesian-optimized random forest (RF) model effectively predicts the inorganic nitrogen adsorption capacity of coral sand, achieving an R² of 0.97, with initial nitrogen concentration and aqueous salinity identified as key input factors. The findings of this study will provide theoretical support for assessing the nitrogen fixation capacity of coral sand on artificial islands and for the development and protection of freshwater lenses in China.