Background and objectiveHeavy metals, ubiquitous in the environment, pose a global public health concern. The correlation between these and diabetic kidney disease (DKD) remains unclear. Our objective was to explore the correlation between heavy metal exposures and the incidence of DKD.MethodsWe analyzed data from the NHANES (2005–2020), using machine learning, and cross-sectional survey. Our study also involved a bidirectional two-sample Mendelian randomization (MR) analysis.ResultsMachine learning reveals correlation coefficients of −0.5059 and − 0.6510 for urinary Ba and urinary Tl with DKD, respectively. Multifactorial logistic regression implicates urinary Ba, urinary Pb, blood Cd, and blood Pb as potential associates of DKD. When adjusted for all covariates, the odds ratios and 95% confidence intervals are 0.87 (0.78, 0.98) (p = 0.023), 0.70 (0.53, 0.92) (p = 0.012), 0.53 (0.34, 0.82) (p = 0.005), and 0.76 (0.64, 0.90) (p = 0.002) in order. Furthermore, multiplicative interactions between urinary Ba and urinary Sb, urinary Cd and urinary Co, urinary Cd and urinary Pb, and blood Cd and blood Hg might be present. Among the diabetic population, the OR of urinary Tl with DKD is a mere 0.10, with a 95%CI of (0.01, 0.74), urinary Co 0.73 (0.54, 0.98) in Model 3, and urinary Pb 0.72 (0.55, 0.95) in Model 2. Restricted Cubic Splines (RCS) indicate a linear linkage between blood Cd in the general population and urinary Co, urinary Pb, and urinary Tl with DKD among diabetics. An observable trend effect is present between urinary Pb and urinary Tl with DKD. MR analysis reveals odds ratios and 95% confidence intervals of 1.16 (1.03, 1.32) (p = 0.018) and 1.17 (1.00, 1.36) (p = 0.044) for blood Cd and blood Mn, respectively.ConclusionIn the general population, urinary Ba demonstrates a nonlinear inverse association with DKD, whereas in the diabetic population, urinary Tl displays a linear inverse relationship with DKD.