It's a combination of the higher gravity and the Earth's magnetic field. Mainly the magnetic field though. Mars would be able to retain its atmosphere too if it had a magnetic field like Earth. It does have some magnetism, but only in patches, remnants from an ancient magnetic field that magnetized some of its rocks, but is now gone.
The MAVEN mission found out that the Mars atmosphere get stripped away far faster during solar storms. Those can't get through the Earth's magnetic fields except at times of magnetic pole reversal when the Earth's magnetic field is almost non existent for a short while.
Earth's gravity is also higher which helps a bit.
But Earth does lose atmosphere also. Just that it does so at a slower rate than Mars. It loses about 95,000 tons of hydrogen every year and about 1,600 tons of helium.
It's just that with Earth's atmosphere of 5148 teratons, this amount of matter loss is negligible. Earth is also gaining mass from meteorites and comets, but not enough to compensate. But we don't need to worry about it, the atmosphere we have would last for trillions of years at this rate. Earth's atmosphere