ImportanceBariatric surgery has been associated with a reduced risk of cancer in individuals with obesity. The association of bariatric surgery with esophageal and gastric cancer is still controversial, however.ObjectiveTo compare the incidence of esophageal and gastric cancer between patients with obesity who underwent bariatric surgery and those who did not (control group).Design, Setting, and ParticipantsThis cohort study obtained data from a national discharge database, including all surgical centers, in France from January 1, 2010, to December 31, 2017. Participants included adults (aged ≥18 years) with severe obesity who underwent bariatric surgery (surgical group) or who did not (control group). Baseline characteristics were balanced between groups using nearest neighbor propensity score matching with a 1:2 ratio. The study was conducted from March 1, 2020, to June 30, 2021.ExposuresBariatric surgery (adjustable gastric banding, gastric bypass, and sleeve gastrectomy) vs no surgery.Main Outcomes and MeasuresThe main outcome was incidence of esophageal and gastric cancer. A secondary outcome was overall in-hospital mortality.ResultsA total of 303 709 patients who underwent bariatric surgery (245 819 females [80.9%]; mean [SD] age, 40.2 [11.9] years) were matched 1:2 with 605 140 patients who did not receive surgery (500 929 females [82.8%]; mean [SD] age, 40.4 [12.5] years). After matching, the 2 groups of patients were comparable in terms of age, sex, and comorbidities (standardized mean difference [SD], 0.05 [0.11]), with some differences in body mass index. The mean follow-up time was 5.62 (2.20) years in the control group and 6.06 (2.31) years in the surgical group. A total of 337 patients had esophagogastric cancer: 83 in the surgical group and 254 in the control group. The incidence rates were 6.9 per 100 000 population per year for the control group and 4.9 per 100 000 population per year for the surgical group, resulting in an incidence rate ratio of 1.42 (95% CI, 1.11-1.82; P = .005). The hazard ratio (HR) of cancer incidence was significantly in favor of the surgical group (HR, 0.76; 95% CI, 0.59-0.98; P = .03). Overall mortality was significantly lower in the surgical group (HR, 0.60; 95% CI, 0.56-0.64; P < .001).Conclusions and RelevanceIn this large, nationwide cohort of patients with severe obesity, bariatric surgery was associated with a significant reduction of esophageal and gastric cancer incidence and overall in-hospital mortality, which suggests that bariatric surgery can be performed as treatment for severe obesity without increasing the risk of esophageal and gastric cancer.