To quantify the association of HIV infection with overdose mortality and explore the potential mechanisms. A prospective cohort study. A total of 1927 actively injecting drug users who were HIV seronegative at baseline, of whom 308 later HIV seroconverted, were followed semi-annually for death from 1988 to 2001. Survival analyses using marginal structural and standard Cox models were used to evaluate the effect of HIV infection on the risk of overdose mortality. Overdose death rates were higher in HIV-seropositive than HIV-seronegative drug users: 13.9 and 5.6 per 1000 person-years, respectively (P < 0.01). The hazard ratio (HR) was 2.54 [95% confidence interval (CI) 1.47, 4.38] for the marginal structural model and 2.06 (95% CI 1.25, 3.38) for the standard Cox model, both adjusted for demographics, drug injection characteristics, alcohol abuse, substance abuse treatment, and sexual orientation. Adjusting for possible time-varying mediators (i.e. drug use, medical conditions and healthcare access) in extended marginal structural models reduced the effect of HIV on overdose mortality by 30% (HR 1.82, 95% CI 1.01, 3.30). Abnormal liver function was associated with a higher risk of overdose mortality (HR 2.00, 95% CI 1.05, 3.84); adjustment for this further reduced the effect of HIV on overdose mortality. HIV infection was associated with a higher risk of overdose mortality. Drug use behavior, systematic disease and liver damage associated with HIV infection appeared to account for a substantial portion of this association. The data suggest a group to target with interventions to reduce overdose mortality rates.