Using a dataset encompassing 228 cities in China spanning from 2005 to 2019, this study explores the nonlinear relationship between air quality and housing prices and devises a strategy that incorporates the instrumental variable and machine learning to address the endogeneity issue. Both traditional models and machine learning models find air pollution affects housing prices in a diminishing manner. The negative impact of air pollution on housing prices decreases when the degree of air pollution intensifies. Such a characteristic is more pronounced in Eastern China and cities with fewer land resource constraints and larger populations. Mechanism analysis also reveals that air pollution could affect residents' perceived air quality and the industrial structure, further contributing to the nonlinear relationship between air quality and housing prices. The further SHapley Additive exPlanations (SHAP) evaluates the importance of air quality in determining housing prices and finds that air quality's contribution outweighs educational and medical resources. The contribution of air quality also shows a distinct regional disparity and has become increasingly important in recent years. The findings refine the benefit assessment accuracy related to air quality improvement.