Ever seen bits of iron fillings arrange themselves in presence of magnet? In very very simple terms, MRI(Magnetic Resonance Imaging) works in the same way.
The magnets excites the Hydrogen present in water molecues. Then these excited Hydrogen align themself acording to the magentic field applied. Then the image is captured of these excited hydrogen. Moving/Changing the magnetic field as needed, its possible to get the 2D or 3D images. Hence this technique is also used in varied fields.