In order to solve the problem that the cesium optical pump magnetometer is disturbed by the carrier’s interference magnetic field during magnetic field anomaly detection, an interference magnetic field compensation method based on an improved mayfly optimization algorithm (IMOA) was proposed in this paper. First, by combining the measurement results of the attitude sensor with the geomagnetic inclination and magnetic declination in the locality, the measurement results of the optical pump magnetometer can be decomposed into the component values under the three axes of the carrier coordinate system. A compensation model including the carrier interference magnetic field was established. Then, considering the poor global search performance that existed in the mayfly optimization algorithm (MOA), an elite chaotic reverse learning strategy and Levy mutation strategy were introduced to improve the MOA. The compensation performance of the IMOA was estimated with a series of field experiments and compared with the stretching particle swarm optimization algorithm. The experiment results indicated that these two methods can effectively compensate the magnetometer’s measurement values, and that the IMOA method more easily jumps out of the local optimum, and has higher compensation accuracy.