When you pull the spring with mass attached to it out a length of d, you're giving the spring potential energy (it's displaced but you're holding it in place). When you release it, it snaps back towards equilibrium.

At the initial position it's potential energy is

U = 1 kd2
2

That is the energy given to the system. We assume (to make the situation simpler) that negligible energy is lost to friction (i.e. we assume that the energy lost is so little that we don't count it as any energy loss). At the position where you want to find the instantaneous velocity, the initial potential energy has become a split of both kinetic and potential energies. But they must add up to the initial energy to imparted on the system:

1 kd2 = 1 mv2 + 1 kx2
2 2 2

solve for v and we get the equation:

v = ± [
k
(d2 - x2)] 1/2
m