Objective To compare the risk for all cause and overdose mortality in people with opioid dependence during and after substitution treatment with methadone or buprenorphine and to characterise trends in risk of mortality after initiation and cessation of treatment. Design Systematic review and meta-analysis. Data sources Medline, Embase, PsycINFO, and LILACS to September 2016. Study selection Prospective or retrospective cohort studies in people with opioid dependence that reported deaths from all causes or overdose during follow-up periods in and out of opioid substitution treatment with methadone or buprenorphine. Data extraction and synthesis Two independent reviewers performed data extraction and assessed study quality. Mortality rates in and out of treatment were jointly combined across methadone or buprenorphine cohorts by using multivariate random effects meta-analysis. Results There were 19 eligible cohorts, following 122 885 people treated with methadone over 1.3-13.9 years and 15 831 people treated with buprenorphine over 1.1-4.5 years. Pooled all cause mortality rates were 11.3 and 36.1 per 1000 person years in and out of methadone treatment (unadjusted out-to-in rate ratio 3.20, 95% confidence interval 2.65 to 3.86) and reduced to 4.3 and 9.5 in and out of buprenorphine treatment (2.20, 1.34 to 3.61). In pooled trend analysis, all cause mortality dropped sharply over the first four weeks of methadone treatment and decreased gradually two weeks after leaving treatment. All cause mortality remained stable during induction and remaining time on buprenorphine treatment. Overdose mortality evolved similarly, with pooled overdose mortality rates of 2.6 and 12.7 per 1000 person years in and out of methadone treatment (unadjusted out-to-in rate ratio 4.80, 2.90 to 7.96) and 1.4 and 4.6 in and out of buprenorphine treatment. Conclusions Retention in methadone and buprenorphine treatment is associated with substantial reductions in the risk for all cause and overdose mortality in people dependent on opioids. The induction phase onto methadone treatment and the time immediately after leaving treatment with both drugs are periods of particularly increased mortality risk, which should be dealt with by both public health and clinical strategies to mitigate such risk. These findings are potentially important, but further research must be conducted to properly account for potential confounding and selection bias in comparisons of mortality risk between opioid substitution treatments, as well as throughout periods in and out of each treatment.