Mercury can stimulate immune responses through T helper 17 (Th17). The gene IL23R is a key factor in Th17 function, which may also contribute to digestive tract diseases. The aim of this study was to observe the associations between dietary mercury and gastric cancer (GC) and to investigate whether the IL23R rs10889677 polymorphism modifies those associations. This case-control study included 377 patients with GC and 756 healthy controls. Dietary mercury intake (total mercury and methylmercury) was assessed using a dietary heavy metal database incorporated into the food frequency questionnaire. IL23R genetic polymorphism rs10889677 (A>C) was genotyped. Odds ratios (OR) and 95% confidence intervals (CI) were calculated using unconditional logistic regression models with adjustments for potential confounders. A higher dietary methylmercury intake was associated with an elevated risk of GC (OR for the highest versus lowest tertile [T3 vs. T1]=2.02; 95% CI, 1.41-2.91; p for trend <0.001). The IL23R rs10889677 reduced the risk of GC in individuals who carried at least 1 minor allele (OR=0.62; 95% CI, 0.46-0.83; p=0.001; AC/CC vs. AA). Individuals with a C allele exhibited a lower susceptibility to GC through methylmercury intake than those with the AA genotype (OR for the T3 of methylmercury and AA carriers=2.93; 95% CI, 1.77-4.87; and OR for the T3 of methylmercury and AC/CC genotype=1.30; 95% CI, 0.76-2.21; p-interaction=0.013). Our findings suggest that a genetic polymorphism, rs10889677 in IL23R, plays a role in modifying the association between dietary methylmercury intake and the risk of GC.