Consider two situations as shown below.
One (left diagram) where two masses are in a non-uniform gravitational field and the other (right diagram) where the two masses are in a unifgorm gravitational filedfield.
In both cases the centre of mass is midway between the two masses $M$.
In the non uniform gravitational filedfield the gravitation attraction on the mass closest to the large mass $W$ is bigger than the gravitational attraction on the other mass $w$, so the centre of gravity of the two masses is at position $G$ which is not midway between the two masses.
In the uniform gravitational filedfield the gravitational attraction on the two masses $W$ is the same fofor the centre of gravity of the two masses is medwaymidway between them $G$ which is the same as the position of the centre of mass.
To be sure that the centre of mass and the centre of gravity are the same point the masses must be in a uniform gravitational field.