Abstract Background Despite growing evidence linking maternal diabetes to impaired brain physiology and neurodevelopment in offspring, evidence on its effect on epilepsy remains scarce. This study aims to determine if intrauterine exposure to maternal pre-existing and gestational diabetes mellitus is associated with risk of offspring epilepsy. Methods We studied >2.3 million singletons live-born in Sweden between 1998 and 2021, with final follow up until October 31, 2023. The Swedish Medical Birth Register was linked to the National Patient Register to identify gestational diabetes (GDM), maternal pre-gestational type 1 (T1DM) and type 2 diabetes (T2DM), and offspring epilepsy. The incidence of epilepsy, comparing offspring of mothers with versus without diabetes, was calculated as hazard ratios (HR), adjusting for maternal education, smoking, body mass index, hypertension, epilepsy and other important confounders. Paternal T1DM and T2DM were additionally analysed as negative control exposures to assess genetic confounding. Results Of the 2,335,245 offspring analysed (48.9% female), 13,883 (0.6%) were exposed to maternal T1DM, 4477 (0.2%) to T2DM, and 37,253 (1.6%) to GDM. During a median follow up of 13 years (range 0-26 years), 18,732 offspring were diagnosed with epilepsy. Maternal T1DM (HR 1.27, 95% confidence intervals [CI] 1.08-1.49) and T2DM (HR 1.34, 95% CI 1.00-1.49) were associated with higher hazards of epilepsy in offspring, compared with no exposure to maternal diabetes. Preterm birth and birth-asphyxia jointly mediated 63% and 39% of these associations, respectively. No association was found between maternal GDM and epilepsy. Paternal T1DM and T2DM did not show association with epilepsy. Conclusions In this nationwide population-based cohort study, children of mothers with T1DM or T2DM showed an elevated risk of epilepsy. The results from paternal comparison and mediation analyses suggest a key role of the intrauterine environment in the aetiology of epilepsy. Key messages • Maternal pre-existing T1DM and T2DM might be risk factors for epilepsy in offspring and are likely to influence the risk through the intrauterine environment independent of genetic mechanisms. • In the context of a globally rising prevalence of diabetes in women who get pregnant, it is crucial that the diabetes is well-regulated and birth complications associated with diabetes are prevented.