• 8-AN, 8-MQ, and 8-QSA chimisorbed on Mg(0001) surface via covalent bonds formation. • Upon the formation of coordination complexes between 8-AN and Mg 2+ , energy gap underwent change about ∆ E = 5.7 eV. • 8-AN, 8-MQ, and 8-QSA in their parallel configurations are found to interact strongly with the Mg atoms. • Electron donor effect of 8-AN compound strongly increases its coordination to Mg(0001) surface. • Interaction energies of all geometries follow the electrochemical performance trends. Heterocyclic compounds are the promising biological compounds as nature-friendly for the corrosion protection of metallic surface. In this work, three heterocyclic compounds such as 1-azanaphthalene-8-ol (8-AN), 2-methylquinoline-8-ol (8-MQ), and 8-quinolinol-5-sulfonic acid (8-QSA) were used as green compounds, and their anti-corrosion performance for AZ31 Mg in saline water was discussed on the basis of impedance interpretation and surface analysis. Findings found that the electrochemical performance was improved in the order of 8-AN > 8-MQ > 8-QSA, demonstrating the electron donor effect of N-heterocycles to form coordination complexes on the magnesium surface. From the electrochemical performance, the protective layer constructed at the optimal concentration reinforces the barrier against aggressive environments, with potential inhibition efficiency of 87.4%, 99.0%, and 99.9% for 8-QSA, 8-MQ, and 8-AN, respectively. Quantum chemical parameters and electron density distribution for free organic species in the absence and presence of Mg 2+ cation were evaluated using density functional theory (DFT). Upon the formation of coordination complexes between organic compound and Mg 2+ , energy gap underwent change about ∆ E = 5.7 eV in the 8-AN/Mg 2+ system. Furthermore, the adsorption of heterocyclic compounds on Mg surface reveals the formation of strong covalent bonds with Mg atoms, which further confirmed by the electron density difference and projected density of states analyses. Based on theoretical calculations, three inhibitors can adsorb on the metal surface in both parallel and perpendicular orientations via C, O and N atoms. In the parallel configuration, the C-Mg, N-Mg and O-Mg bond distances are between 2.11 and 2.25 Å, whereas the distances in the case of perpendicular adsorption are between 2.20 and 2.40 Å (covalent bonds via O and N atoms). The results indicated that parallel configurations are energetically more stable, in which the adsorption energies are -4.48 eV (8-AN), -4.28 eV (8-MQ) and -3.82 eV (8-QSA) compared to that of perpendicular adsorption (-3.65, -3.40, and -2.63 eV). As a result, experimental and theoretical studies were in well agreement and confirm that the nitrogen and oxygen atoms will be the main adsorption sites.