This paper investigates the magnetic filed interference problem when the ROV equipped with a three-axis magnetometer measures the magnetic field of underwater magnetic targets within a short range, and a magnetic field compensation method based on an improved mayfly algorithm is proposed to improve the measurement accuracy of underwater magnetic field information. Firstly, a compensation model is established based on the installation error of the three-axis magnetometer and the interference magnetic field of the ROV. Then, in view of the problem that the original mayfly algorithm is easy to fall into local optimal and the convergence accuracy is poor, the Tent chaotic sequence and the Levy flight mutation strategy are introduced to improve the original mayfly algorithm. Finally, a series of magnetic field information is obtained through the three-axis magnetometer, and the original mayfly algorithm, particle swarm algorithm and improved mayfly algorithm are used to estimate the compensation parameters. The experimental results show that the improved mayfly algorithm has obtained faster convergence speed and higher compensation accuracy than others.