The impact of air-quality ecological compensation on total factor energy efficiency can help a country to achieve high-quality economic development with the goals of peak carbon emissions and carbon neutrality, and to explore a win–win path for the economy and the environment. This study investigates the impact of air-quality ecological compensation (AQEC) on total factor energy efficiency using the time-varying difference-in-difference model with a sample of 282 Chinese cities from 2004 to 2022. The results show AQEC significantly increases total factor energy efficiency by 1.71% in the pilot areas. This result remains robust after undergoing PSM-DID, considering only the first three pilot batches, an instrumental variable approach, the exclusion of other policies interference, and dual machine learning. The mechanisms analysis indicates that AQEC enhances total factor energy efficiency by promoting industrial structure advancement and green technology innovation. Furthermore, it is noteworthy that heterogeneity exists in the effect of AQEC on total factor energy efficiency, particularly in cities with an old industrial base and small cities. Overall, this study refines the causal relationship between air-quality ecological compensation and total factor energy efficiency, providing empirical evidence and policy insights for China and other countries to enhance energy efficiency and promote urban ecological civilization.