BackgroundThe relationship between vitamin D levels, depressive symptoms, and cognitive function has yet to be definitively understood in the elderly, particularly when considering the impact of chronic diseases. This study focuses on how depression mediates the impact of 25-hydroxyvitamin D3 (25(OH)D3) on cognitive performance in older U.S. adults.MethodsWe analyzed data from 2,745 elderly individuals extracted from the NHANES 2011–2014 cycles, applying weighted processing to account for the complex multi-stage sampling design characteristic of NHANES data. Utilizing weighted data for covariate and model selection, we conducted mediation analyses on both the overall population and subgroup data. Significant mediation pathways were validated using a stratified weighted bootstrap approach. For significant subgroup pathways, we explored interactive mechanisms through interactive mediation analysis.ResultsMediation analyses, thoroughly accounting for the impact of chronic conditions, revealed significant pathways in both the weighted overall population and the weighted diabetes subgroup. After 1,000 stratified weighted bootstrap replications, the proportion of mediation effects were 10.6% [0.040, 0.268] and 20.9% [0.075, 0.663], respectively. Interactive mediation analysis for diabetes indicated that the interaction between diabetes and depression was not significant in the direct pathway (estimates = 0.050, p = 0.113) but was significant in the mediation pathway, yielding the largest effect size compared to other covariates (estimates = 0.981, p < 0.001).ConclusionThis study highlights the mediating role of depression in the relationship between vitamin D levels and cognitive function in the elderly, particularly emphasizing diabetes as a key moderator. Our findings suggest targeted interventions addressing both vitamin D sufficiency and depression could significantly benefit cognitive health, especially in diabetic individuals.