The coefficient on X in Y ~ X is the total effect of X on Y. The coefficient on X in Y ~ M + X is the direct effect of X on Y. The total effect is the sum of the direct and indirect effects, where the indirect effect is the effect of X on Y through M.
In your situation, there is no total effect of X on Y, but there is a direct effect on X on Y. This can occur when there are two opposing causal pathways from X to Y. For example, it has been observed that wearing a helmet does little to prevent injury to cyclists (i.e., the total effect of wearing a helmet on injury is zero). This could be explained by the fact that helmets encourage riskier behavior by cyclists (i.e., because they feel safer), thereby increasing the risk of injury, while the helmets themselves provide safety to the cyclists, decreasing the risk of injury. The indirect effect (the effect through risky behavior) and the direct effect (the effect due to the safety provided by the helmets) are in opposite directions, yielding a total effect of zero, even though there is a nonzero direct effect.
Of course, what you observed has occurred in a sample, and so failing to find a significant effect doesn't mean there is no effect. It just means you might not have the precision to detect it. Direct effects can often be estimated with much more precision that total effects, so it could just be that your estimate of the total effect isn't precise even though a total effect is present.