I'm not exactly sure what you mean by "equilibrium with the cavity", but radiation and matter was in thermal equilibrium until decoupling 379,000 yr after Big Bang, i.e. photons and particles shared the same distribution in energy. This energy was given by the temperature, so the spectrum of the photons was a Planck spectrum, characteristic of a blackbody.
The "recombination" of protons and electrons, together with the expansion of the Universe, depleted space of its main scattering agent, electrons, over a rather short period of time. In a few $10^4$ yr, the scattering rate of photons went from being significantly shorter than the expansion rate of the Universe, to being significantly longer. The is equivalent to saying that the photons' mean free path went from being shorter than the size of the observable Universe, to being longer.
After this, (most of) the photons haven't been interacting with anything, so they are still exactly the same photons that were released at that time, only they have redshifted on their way through the expanding Universe. That means that they still exhibit the same Planck spectrum as then, only redshifted.
You might think that because the photons didn't decouple exactly simultaneously, the spectrum shouldn't be an exact Planck spectrum, but be "smeared out" a little. But the photons have always been subjected to a redshift, also prior to, and during, decoupling. When the Universe was, say, 370,000 yr old, it was 2987 K hot (using a Planck 2016 cosmology), and so its spectrum peaked at
$$
\lambda_\mathrm{peak,370\,kyr} = \frac{b}{2\,987\,\mathrm{K}} = 970\,\mathrm{nm},
$$
where $b$ is Wien's displacement constant.
On the other hand, photons that decoupled late, say when the Universe was 390,000 yr and the temperature had fallen to 2895 K, would peak at
$$
\lambda_\mathrm{peak,390\,kyr} = \frac{b}{2\,895\,\mathrm{K}} = 1\,001\,\mathrm{nm}.
$$
But in the 20,000 yr that went from photon A decoupled with $\lambda = 970\,\mathrm{nm}$ till photon B decoupled with $\lambda = 1001\,\mathrm{nm}$,
photon A has redshifted to $\lambda = 1001\,\mathrm{nm}$, so it is indistinguishable from a photon B.
This is why the CMB is described by such a perfect blackbody curve.