When an object (i.e.magnetic) is attracted towards the magnet, work is done so that the energy is used but from where this energy comes from?
The energy is "stored" in the configuration of the electromagnetic field. When the positions of the magnets are changed the configuration of the electromagnetic field changes accordingly. If the magnets are forced into a new position, then energy is transferred to the EM field. If the magnets spontaneously change position and acquire some kinetic energy, this kinetic energy increase came from a decrease in the EM field energy.
To get a visual sense of this visit MIT's Physics 8.02 TEAL Electromagnetic visualization and animation website: http://web.mit.edu/8.02t/www/802TEAL3D/visualizations/magnetostatics/MagneticForceRepel/MagneticForceRepel.htm
This change in energy can be calculated explicitly by the volume integration of the energy density of the field (multiplied by constant). [There are other methods of calculation]
Your question seems to imply that is the magnetic field is doing the work. Strictly speaking, magnetic fields can't do work. I don't know how far along you are in your studies but magnetic fields are a consequence of relativity theory and in the final analysis it will always be found that work is done by the electric field.
The question is like asking "when a body falls on the ground from a height, work is done and energy is used. but where does this energy come from?"
The answer is, that force-fields (magnetic, electric, gravitational) provide a means for suitable materials to jump from a higher energy state to a lower energy state.
When a magnet is present in the vicinity of another magnet and not attached to it, it the state high energy (for the whole system). When the lighter magnet jumps and attaches to the heavier magnet, this is the lower energy state. Yes, work is done and energy is spent. This energy has been borrowed from the system, bringing the whole system to a lower energy state. If you want to put the system back into former position, you must spend energy in the opposite direction. This is the repayment of the loan of energy which was taken earlier.
When two magnetic object attract or repel it is because at that certain distance the electron movements oppose one another. When the magnets move toward eachother their potential energy from their attractuion is lost, resulting in equilibrium. The work that moves the magnets is equal to the potential energy of the magnets while seperated before they move.