Magnesium (Mg) alloys have shown great promise as engineering materials due to their unique properties. However, their high chemical reactivity limits their wide application. Herein, a novel bilayer diatomite-based composite coating was fabricated by spraying coating AZ31B Mg alloy with a bottom layer of benzotriazole (BTA)-loaded diatomite/epoxy resin for providing self-healing properties and a top layer of hexadecyltrimethoxysilane (HDTMS)-modified diatomite/epoxy resin for contributing superhydrophobic properties. The resulting bilayer composite coating (BTA@DME-12 %) exhibited exceptional stability and maintained its superhydrophobicity in a temperature range of 0–160 °C. The coating also delivered good adhesion and physical stability in tape-peeling and frictional testing. Electrochemical study results demonstrated that the BTA@DME-12 % coating exhibited lower corrosion tendencies and corrosion rates compared to the BTA@DME-free coatings. After immersing in 3.5 wt% NaCl solution for 21 days, the bilayer composite coating still achieved an impressive anti-corrosion efficiency of 99.6 %. This work not only provides a scalable and simple method to prepare self-healing and superhydrophobic bifunctional coatings for corrosion protection of Mg alloy, but also presents a novel multifunctional coating strategy by utilizing diatomite with a hierarchical structure.