MRI works by aligning the magnetic moments of hydrogen nuclei in water molecules within the body using a powerful magnet. When a radiofrequency pulse is applied, these nuclei are temporarily excited to a higher energy state. As they relax back to their base state, they emit signals that are detected and translated into images by a computer.