this is because if something is collapsing from gravity it will stop once some other force is pushing out harder.
this explains a lot of phenomena and is a key piece of understanding the modern theories of stellar phenomena, e.g. via the pauli exclusion principle, why neutron stars can form during a supernova (from the extra inward pressure combining with the gravitational collapse exceeding the degeneracy pressure of electrons in the plasma)... its also why beyond a critical mass the collapse doesn't stop at all and produces a black hole.
in any case energy is always conserved... but the ultimate place it is being extracted from to power a star is the gravitational field. everything else, e.g. the nuclear fusion, the light pressure from temperature etc. is a result of that energy being transferred into the star as it forms and continues to evolve.