Hint: Whenever we extend or compress a spring of spring constant$$k$$, spring exerts a force on us that opposes the change in length of spring.
Step 1: Calculate the initial and Final Energy of the system.
Let the mass of two identical blocks attached to two ends of spring is $$m$$. Let spring constant of spring is $$k$$, and length of spring is $$L$$.
When the spring is in its original length, the Energy stored in the system is, $$E_1 = 0$$.
When spring is stretched by length x, by pulling both the blocks symmetrically, Energy stored in the system is given by,
$$E_2 = \dfrac{1}{2}kx^2$$
Step 2: Calculate Work done by spring.
By conservation of Potential Energy, we know that work done by a conservative force is equal to change in Potential Energy of the system.
Therefore, work done by spring is,
$$ W = -(\dfrac{1}{2}kx^2 - 0)$$
$$\Rightarrow W = -\dfrac{1}{2}kx^2$$
Since, both the blocks are pulled symmetrically, work done by spring on each block will behalf of this work done.
Therefore Work done by spring on each block is,
$$w = -\dfrac{1}{4}kx^2$$
Thus, Work done by spring on each mass is, $$w = -\dfrac{1}{4}kx^2$$
Option D is correct.