We analyze the mass distribution of cores formed in an isothermal, magnetized, turbulent, and self-gravitating nearly critical molecular cloud model. Cores are identified at two density threshold levels. Our main results are that the presence of self-gravity modifies the slopes of the core mass function (CMF) at the high mass end. At low thresholds, the slope is shallower than the one predicted by pure turbulent fragmentation. The shallowness of the slope is due to the effects of core coalescence and gas accretion. Most importantly, the slope of the CMF at the high mass end steepens when cores are selected at higher density thresholds, or alternatively, if the CMF is fitted with a log-normal function, the width of the lognormal distribution decreases with increasing threshold. This is due to the fact that gravity plays a more important role in denser structures selected at higher density threshold and leads to the conclusion that the role of gravity is essential in generating a CMF that bears more resemblance with the IMF when cores are selected with an increasing density threshold in the observations.