The whole universe was originally filled with hot ionised hydrogen and helium that were the products of nucleosynthesis in the BB. The energy for the radiation is thought to have come from the epoch of matter-antimater annihilation where (108 + 1) matter particles annihilated 108 anti-matter particles leaving ~108 photons per atomic particle in the present universe.
That radiation was bounced around the very early and opaque universe until the plasma termperature fell to ~ 30000K when the protons and electrons associated to form neutral hydrogen atoms, then the universe became transparent.
We see that 'Surface of Last Scattering' all around us and now red-shifted by z ~ 1100 as the CMB.
The plasma and subsequent gas was remarkably homogeneous and isotropic and therefore its radiation follows a Planck black-body spectrum very closely.
There are anisotropies, of course, but they are another story.
Garth