Within the framework of self-force theory, we compute the gravitational-wave energy flux through second order in the mass ratio for compact binaries in quasicircular orbits. Our results are consistent with post-Newtonian calculations in the weak field and they agree remarkably well with numerical-relativity simulations of comparable-mass binaries in the strong field. We also find good agreement for binaries with a spinning secondary or a slowly spinning primary. Our results are key for accurately modelling extreme-mass-ratio inspirals and will be useful in modelling intermediate-mass-ratio systems.