How can work done be equal and opposite when the forces are unequal and object travels the same distance Let's suppose we have a spring fixed to a wall, attached to a block and nothing is said about the forces acting on the block or spring.  It's known that if the block is displaced in the positive direction, the work done on the spring will be equal to $\frac12 kx^2$ and the work done by the spring will be $-\frac12 kx^2$.  How can the work done be equal and opposite when to displace an object the forces required should be unequal?
 A: Newton’s third law tells us that the force exerted by the block on the spring is always equal in magnitude and opposite in direction to the force exerted by the spring on the block. These forces are internal to the block/spring system.
There may be other external forces acting on the block and/or on the spring, and the block and the spring may or may not be in equilibrium. None of this affects the balance of the internal forces.
A: 
How can work done be equal and opposite when the forces are unequal
and object travels the same distance

When you say “the forces are unequal” you need to distinguish between the action-reaction pairs of forces acting on the block per Newton’s 3rd law,  and net force acting on the block, per Newton’s 2nd law.
There are two action-reaction pairs acting on the block: Spring-block and External force-block. These are equal and opposite forces per Newton’s 3rd law and always apply, whether or not the block is displacing the spring.
The net force acting on the block is the difference between the external (to the spring-block system) force acting on the block, say you pushing or pulling on the block, and the restorative force the spring exerts on the block. It is this net force (unequal external and spring forces) acting on the block that is necessary to initiate its displacement per Newton's 2nd law. But those forces need only be unequal at the beginning and end of the displacement of the block. To explain:
Consider the simple example of a horizontally oriented ideal spring fixed to a wall at one end and connected to a block at the other, with the block resting on a frictionless surface.
To initiate movement of the block, an external (to the block-spring system) force slightly greater than the restorative force of the spring is applied to give the block a small acceleration. Once movement begins, the force is adjusted to equal the spring force causing the block to move at constant velocity. Then, just prior to reaching the final desired displacement, the external force on the block is made slightly less than the spring force, causing the block to decelerate and come to rest. The average force on the block is then zero.
Now consider the work done on the block:
Since the direction of the external force is always the same as the displacement of the block (during compressing or extension of the spring) the external force does positive work of $+\frac{1}{2}kx^2$ on the block. Positive work  transfers energy to an object.
Since the direction of the spring restoring force is always opposite to the direction of the displacement of the block, the spring does negative work of $-\frac{1}{2}kx^2$. When negative work is done on an object, energy is removed from the object. In this case, the spring removes the energy given the block by the external agent and stores it at elastic potential energy of the block-spring system.
The above follows from the work energy theorem which states that the net work done on an object equals its change in KE. Since the block begins and ends at rest, its change in KE is zero. Thus the net work done is zero.
The gravity analogy is an external agent raising a block from rest on the ground and bringing it to rest at some height $h$ from the ground. The external agent does positive work on the block and gravity does an equal amount of negative work on the object, storing the energy as gravitational potential energy of $mgh$ in the block-Earth system.
Hope this helps.
A: Two possibilities: (1) The block is moving as it starts compressing the spring.  The spring does negative work on the block and removes its kinetic energy, and the block does positive work on the spring, giving it potential energy. (2) The block starts at rest and is pushed by an external force.  The force does positive work and the spring does negative work on the block. Its kinetic energy does not change.  Again, the block does positive work on the spring.  In each case, the spring gains energy.
