Pure iron and low carbon steel generally don't become magnetized. Unfortunately, the tool steel we use has carbon and is susceptible. The magnetic domains in steel are normally randomly oriented so the net alignment is zero and the steel is not magnetized. When a magnet is brought near, the domains align along the magnetic field line produced by the magnet. (Visualize some iron filings brought near a magnet. The filings will align themselves in more or less parallel strings going from one pole of the magnet to the other.)
If the magnet is suddenly pulled away from the steel, the domains retain the alignment and the steel is magnetized. As a youngster, I used to stoke a sewing needle to magnetize it and if it were placed on a cork in a bowl of water, it would become a compass. Magnetic fields fall off rather quickly as the distance from the magnet increases. Doubling the distance will reduce the field strength to 1/8th.
Years ago, magnets were fairly weak. The rare earth magnets that surround us in our daily live are much stronger. The act of casually bringing a piece of steel near one of these is sufficient to magnetize the steel. The magnetic fields created by coils of wire carrying current can also magnetize steel if the current is suddenly switched off. Using the indicator stands with magnetic bases is another good way to unintentionally magnetize steel.
Steel can be demagnetized by placing in an oscillating magnetic field and slowly withdrawing it. The oscillations can be done by applying an alternating current to a coil of wire. The strength of the field created is proportional to the current times the number of turns. This is the method used by many demagnetizers. Another way is using a rotating magnet. I had a cassette tape demagnetizer that just used a bar magnet attached to a small motor. In either case, the magnetized steel is brought into the magnetic field and slowly withdrawn.
I have repurposed on of my soldering guns as a demagnetizer. One interesting point is, since the magnetic field strength is proportional to the current times the number of turns but inversely proportional to the length of the wire, increasing the number of turns didn't increase the magnetic field. For my 140w. gun, I use about 6" of 10AWG wire for soldering tips. Resistance halves for every three AWG wires sizes so a 14" length of 6AWG wire has about the same resistance. I made adapters for the lugs on my soldering gun and flattened and punched eyes in the end of the wire. I annealed and bent it into two 2" i.d. loops. On the 100 watt setting, I pegged the 300 amp range on the 100 watt position my clamp on ammeter so I am probably close to 500 amps on the 140 watt position. At two turns, this gives me 1,000 ampere-turns.
To use, I power the soldering gun and slowly pass the magnetized piece through the coils and pull it away from the coils before releasing the trigger. A single pass is sufficient.