Hospitalizations related to opioid use disorder (OUD) represent an opportunity to initiate medication for OUD (MOUD). To assess whether starting MOUD after a hospitalization or emergency department (ED) visit is associated with the odds of fatal and nonfatal opioid overdose at 6 and 12 months. This population-based cohort study used data from the Oregon Comprehensive Opioid Risk Registry, which links all payer claims data to other administrative health datasets, for individuals aged 18 years or older who had diagnosis codes related to OUD recorded at an index ED visit or hospitalization from January 2017 to December 2019. Data were analyzed between May 2023 and January 2024. Receipt of MOUD within the 7 days after an OUD-related hospital visit. The primary outcome was fatal or nonfatal overdose at 6 and 12 months after discharge. Sample characteristics, including age, sex, insurance plan, number of comorbidities, and opioid-related overdose events, were stratified by receipt or nonreceipt of MOUD within 7 days after an OUD-related hospital visit. A logistic regression model was used to investigate the association between receipt of MOUD and having an opioid overdose event. The study included 22 235 patients (53.1% female; 25.0% aged 25-39 years) who had an OUD-related hospital visit during the study period. Overall, 1184 patients (5.3%) received MOUD within 7 days of their ED visit or hospitalization. Of these patients, 683 (57.7%) received buprenorphine, 463 (39.1%) received methadone, and 46 (3.9%) received long-acting injectable naltrexone. Patients who received MOUD within 7 days after discharge had lower adjusted odds of fatal or nonfatal overdose at 6 months compared with those who did not (adjusted odds ratio [AOR], 0.63; 95% CI, 0.41-0.97). At 12 months, there was no difference in adjusted odds of fatal or nonfatal overdose between these groups (AOR, 0.79; 95% CI, 0.58-1.08). Patients had a lower risk of fatal or nonfatal overdose at 6 months associated with buprenorphine use (AOR, 0.50; 95% CI, 0.27-0.95) but not with methadone use (AOR, 0.57; 95% CI, 0.28-1.17). In this cohort study of individuals with an OUD-related hospital visit, initiation of MOUD was associated with reduced odds of opioid-related overdose at 6 months. Hospitals should consider implementing programs and protocols to offer initiation of MOUD to patients with OUD who present for care.