As indicated by the previous poster you will clear Mexican immigration at MEX. Customs inspection, which is random, takes place at CUN. You won't see your luggage until Cancun.
Connecting passengers at MEX after clearing Mexican immigration are routed back airside (there are plenty of Spanish/English signs directing you the right way). Just prior to re-entering the terminal you'll go through a security checkpoint with a WTMD and X-ray machine.