Since you have to push the magnets to get them close together, you're doing work on them- putting energy in. If they fly apart, that energy is released, maybe in the form of the motion of the the two magnets. The situation is very similar to what happens when you push the ends of a spring together. When they spring apart, the energy you put in is released.
So you can store a little energy in some magnets. However, the devices you occasionally hear advertised that are supposed to keep pulling energy out of magnets forever are fraudulent.
If one magnet is permanent and the other is an electromagnet, you can get the system to do mechanical work by constantly alternating the current passing through the electromagnet. The permanent magnet and the electromagnet will alternately attract and repel, and the resulting back-and-forth motion can be made to do something useful (like ring a doorbell). In fact, this is a simplified version of how electric motors work
. Electrical energy is transformed into mechanical energy by this.
If both magnets are permanent magnets as you ask, it will push away only once.
Mike W. (and Tom)
(published on 10/22/2007)