Background Bariatric surgery is associated with a significant improvement in depressive mood in the initial postoperative years, but the maintenance of the improvement is under debate. Aim To explore the association between bariatric surgery and major depressive disorder (MDD) in a 12-year nationwide cohort study. Method Using the National Health Insurance Research Database of Taiwan, we identified 2302 patients who underwent bariatric surgery in 2001-2009. These patients were matched by propensity score to 6493 obese patients who did not receive bariatric surgery. We followed the surgical and control cohorts until death, any diagnosis of MDD or December 31, 2012. We used Cox proportional hazard regression models to calculate the relative risk of MDD in those who received bariatric surgery. Results Overall, there was a 1.70-fold (95% CI: 1.27-2.27) higher risk of MDD in the surgical group. Subjects receiving malabsorptive procedures showed a higher risk of MDD (3.01, 95% CI: 1.78-5.09) than those receiving restrictive procedures (1.51, 95% CI: 1.10-2.07). Stratified by follow-up period, there was a higher risk of MDD in the surgical group (2.92, 95% CI: 1.75-4.88) than in the restrictive group four years after bariatric surgery. Conclusions Bariatric surgery was significantly associated with an elevated risk of MDD.