The association between gestational diabetes mellitus (GDM) and incident kidney disease, the mediating effects of diabetes and hypertension, and the impact of severity of metabolic dysfunction during pregnancy on the risk of incident kidney disease were investigated in this study. This Danish, nationwide, register-based cohort study included all women giving birth between 1997 and 2018. Outcomes included chronic kidney disease (CKD) and acute kidney disease, based on diagnosis codes. Cox regression analyses explored the association between GDM and kidney disease. A proxy for severity of metabolic dysfunction during pregnancy was based on GDM diagnosis and insulin treatment during GDM in pregnancy and was included in the models as an interaction term. The mediating effects of subsequent diabetes and hypertension prior to kidney disease were quantified using mediation analyses. Data from 697,622 women were used. Median follow-up was 11.9 years. GDM was associated with higher risk of CKD (adjusted hazard ratio [aHR] 1.92; 95% CI 1.67-2.21), whereas acute kidney disease was unrelated to GDM. The proportions of indirect effects of diabetes and hypertension on the association between GDM and CKD were 75.7% (95% CI 61.8-89.6) and 30.3% (95% CI 25.2-35.4), respectively, as assessed by mediation analyses. The CKD risk was significantly increased in women with insulin-treated GDM and no subsequent diabetes compared with women without GDM (aHR 2.35; 95% CI 1.39-3.97). The risk of CKD was significantly elevated after GDM irrespective of subsequent development of diabetes and hypertension. Furthermore, women with severe metabolic dysfunction during pregnancy had the highest CKD risk.