When a spring is kept in a magnetic field and current is passed through it, its edges act as parallel conductors and due to the action of Lorentz force, they pull themselves together. A spring contracts. As far as I know, magnetic field is non-conservative and cannot do work itself. i.e. It will only change direction of a moving body but never accelerate or decelerate it.
Then, from where does the elastic potential energy in the spring come from?