This study aimed to investigate the association between serum 25-hydroxyvitamin D (25(OH)D) concentrations and mortality in long-term prescription opioid users. The study included 1856 long-term prescription opioid users from the National Health and Nutrition Examination Survey (NHANES, 2001-2018). Mortality status were determined by matching with the National Death Index (NDI) records until December 31, 2019. Multivariable Cox proportional hazard models were constructed to assess the association. Over a median follow-up period of 7.75 years, there were 443 cases of all-cause mortality, including 135 cardiovascular disease (CVD) deaths and 94 cancer deaths. After multivariable adjustment, participants with serum 25(OH)D concentrations within 50.00 to <75.00 nmol/L and ≥ 75 nmol/L had a lower risk of all-cause mortality, with hazard ratios (HRs) of 0.50 (95% confidence interval [CI] 0.29, 0.86) and 0.54 (95% CI 0.32, 0.90), respectively. Nevertheless, no significant association was found between serum 25(OH)D concentrations and the risk of CVD or cancer mortality. The RCS analysis revealed a non-linear association of serum 25(OH)D concentration with all-cause mortality (p for non-linear = 0.01). Per 1-unit increment in those with serum 25(OH)D concentrations <62.17 nmol/L corresponded to a 2% reduction in the risk of all-cause mortality (95% CI 0.97, 1.00), but not changed significantly when 25(OH)D concentrations ≥62.17 nmol/L. In conclusion, a non-linear association existed between serum 25(OH)D concentrations and all-cause mortality in long-term prescription opioid users. Maintaining serum 25(OH)D concentrations ≥62.17 nmol/L may be beneficial in preventing all-cause mortality in this population.