Vitamin D has been suggested to protect against depression, but epidemiological evidence is scarce. The present study investigated the relationship of serum 25-hydroxyvitamin D (25(OH)D) with the prevalence of depressive and anxiety disorders. The study population consisted of a representative sample of Finnish men and women aged 30–79 years from the Health 2000 Survey. The sample included 5371 individuals, of which 354 were diagnosed with depressive disorder and 222 with anxiety disorder. Serum 25(OH)D concentration was determined from frozen samples. In a cross-sectional study, a total of four indicators of depression and one indicator of anxiety were used as dependent variables. Serum 25(OH)D was the risk factor of interest, and logistic models used further included sociodemographic and lifestyle variables as well as indicators of metabolic health as confounding and/or effect-modifying factors. The population attributable fraction (PAF) was estimated. Individuals with higher serum 25(OH)D concentrations showed a reduced risk of depression. The relative odds between the highest and lowest quartiles was 0·65 (95 % CI 0·46, 0·93; P for trend = 0·006) after adjustment for sociodemographic, lifestyle and metabolic factors. Higher serum 25(OH)D concentrations were associated with a lower prevalence of depressive disorder especially among men, younger, divorced and those who had an unhealthy lifestyle or suffered from the metabolic syndrome. The PAF was estimated to be 19 % for depression when serum 25(OH)D concentration was at least 50 nmol/l. These results support the hypothesis that higher serum 25(OH)D concentrations protect against depression even after adjustment for a large number of sociodemographic, lifestyle and metabolic factors. Large-scale prospective studies are needed to confirm this finding.