When we compress a spring, we are applying force to do so. The mechanical energy done by us is being converted to potential energy stored in the compresses state of the spring.
Similar to when we put a stone in the catapult. Mechanical energy is being transformed to potential energy.