Why does a spring lose a part of its energy when compressed for a long period of time? Is it because the material gets bent?
Yes. Some of the elastic energy stored in the spring does work by moving lattice dislocations through the metal - this is the physical mechanism responsible for the plastic deformation of the metal spring - and is the reason the spring may be permanently deformed when unloaded, even when the grip position applied to the spring has remained fixed. Plastic deformation generates heat, which can be lost to the environment as the deformed spring cools.
If it seems difficult to understand how work is being done when the ends of the spring are fixed, remember that the stress field inside the spring is inhomogeneous. Where the local stress exceeds the yield stress there are plastic strains that correspond to real displacements within the metal lattice, which are doing work against the local stress field. You could use calipers to measure the diameter of the wire comprising the spring to demonstrate that the material has changed shape.
The phenomenon you're looking at is creep, in which a metal held at a constant strain will gradually relax and so reduce the stress. This will reduce the stored strain energy. It has nothing to do with fatigue, which is the result of repeated cyclic loading, and it has nothing to do with the homogeneity of the stress field. The Wikipedia article 'Creep' is informative.
When you compress a spring you are doing work on it and it gains elastic potential energy. now, if you release it, this energy is utilised to restore its original form. so the elasticity of the spring is the source of the restoring force. If you keep it compressed for a long time "elastic fatigue" occurs due to which its behaviour becomes less elastic, ie, it does not return to its original configuration, hence deformed(or bent).
The repetitive bending and unwinding of a spring causes something called elastic fatigue: that implies loss in elastic nature of the spring. So now that spring won't be able to bear the same value of elastic potential energy as it used to. That's the precise answer, elastic fatigue occurs because repetitive winding and unwinding disturbs molecular arrangement of the system and thus, it loses its elastic property.