How does an MRI work? This is my interpretation of an MRI:
There's an external magnetic field and most of the hydrogen nuclei will align themselves with that field (which is the lower energy state) but some are not aligned (which is the higher energy state). A chemist can then direct a wide range of radio waves with different wavelengths. The specific wavelength that causes the hydrogen nuclei to go from their lower energy aligned state to their higher energy unaligned state can be determined by the wavelength of the energy emitted by the photon when the hydrogen nuclei return to its ground state. And that wavelength determines the environment. Is this accurate?
The simplest way to detect where a spin in a sample is is to place this in a magnetic field that changes in direct proportion to depth. Then the frequency at which spins respond to the r.f. radiation can be interpreted as a depth. Clearly it is going to be far more complex to get 3D images but in essence is the same; the detected signal has to be encoded with a position.