An electromagnet is a soft metal core made into a magnet by the passage of electric current through a coil surrounding it. It works because an electric current produces a magnetic field. The magnetic field produced by an electric current forms circles around the electric current
William Sturgeon invented the first electromagnet by using a horseshoe-shaped piece of iron that was wrapped with a loosely wound coil of several turns. When a current was passed through the coil; the electromagnet became magnetized and when the current was stopped the coil was de-magnetized. Sturgeon displayed its power by lifting nine pounds with a seven-ounce piece of iron wrapped with wires through which the current of a single cell battery was sent.
Electromagnets - How can electricity create a magnet?