To assess the association between diet cost and quality by place of residence. We analysed cross-sectional data of the National Health and Nutrition Survey-2012. Diet cost was estimated by linking dietary data, obtained from a 7-d SFFQ, with municipality food prices, which were derived from a national expenditure survey. Diet quality was assessed using the Healthy Eating Index-2015 (HEI-2015). Association between quintiles of diet cost and HEI-2015 was assessed using linear regression analysis. Mexico. 2438 adults (18-59 years). Diet cost was positively associated with diet quality (HEI-2015) in urban but not in rural areas. Compared with quintile (Q1) of cost, the increment in diet quality score was 1·17 (95 % CI -0·06, 4·33) for Q2, 2·14 (95 % CI -0·06, 4·33) for Q3, 4·70 (95 % CI 2·62, 6·79) for Q4 and 6·34 (95 % CI 4·20, 8·49) for Q5 (P-trend < 0·001). Individuals in rural v. urban areas on average have higher quality diets at lower cost with higher intakes of whole grains and beans and lower intakes of Na, added sugars and saturated fats. Living in the South, being indigenous and having low socio-economic status were also associated with higher quality diets. Diet cost was positively associated with diet quality, but only in urban areas. Further studies are needed to understand the relation between diet cost and quality in rural areas. To improve overall diet quality in Mexico, strategies that aim to reduce the cost of high-quality diets should consider the heterogeneity by place of residence.