An object floats when the density of the floating body is lesser than the density of the liquid it floats on and vice versa.
The density of mercury higher than iron, while the density of iron is higher than water. Therefore, iron floats in mercury, but sinks in water.