The energy is stored in the spring in the form of potential energy when it is stretched or compressed. The spring advances towards its equilibrium position with the same potential energy as you leave it, converting it into kinetic energy.
The stored potential energy formula:
Where denotes the elastic potential energy in joules, denotes the spring constant in newtons per meter, and is the length extension in meters.