IMPORTANCE: Associations have been found between COVID-19 and subsequent mental illness in both hospital- and population-based studies. However, evidence regarding which mental illnesses are associated with COVID-19 by vaccination status in these populations is limited. OBJECTIVE: To determine which mental illnesses are associated with diagnosed COVID-19 by vaccination status in both hospitalized patients and the general population. DESIGN, SETTING, AND PARTICIPANTS: This study was conducted in 3 cohorts, 1 before vaccine availability followed during the wild-type/Alpha variant eras (January 2020-June 2021) and 2 (vaccinated and unvaccinated) during the Delta variant era (June-December 2021). With National Health Service England approval, OpenSAFELY-TPP was used to access linked data from 24 million people registered with general practices in England using TPP SystmOne. People registered with a GP in England for at least 6 months and alive with known age between 18 and 110 years, sex, deprivation index information, and region at baseline were included. People were excluded if they had COVID-19 before baseline. Data were analyzed from July 2022 to June 2024. EXPOSURE: Confirmed COVID-19 diagnosis recorded in primary care secondary care, testing data, or the death registry. MAIN OUTCOMES AND MEASURES: Adjusted hazard ratios (aHRs) comparing the incidence of mental illnesses after diagnosis of COVID-19 with the incidence before or without COVID-19 for depression, serious mental illness, general anxiety, posttraumatic stress disorder, eating disorders, addiction, self-harm, and suicide. RESULTS: The largest cohort, the pre-vaccine availability cohort, included 18 648 606 people (9 363 710 [50.2%] female and 9 284 896 [49.8%] male) with a median (IQR) age of 49 (34-64) years. The vaccinated cohort included 14 035 286 individuals (7 308 556 [52.1%] female and 6 726 730 [47.9%] male) with a median (IQR) age of 53 (38-67) years. The unvaccinated cohort included 3 242 215 individuals (1 363 401 [42.1%] female and 1 878 814 [57.9%] male) with a median (IQR) age of 35 (27-46) years. Incidence of most outcomes was elevated during weeks 1 through 4 after COVID-19 diagnosis, compared with before or without COVID-19, in each cohort. Incidence of mental illnesses was lower in the vaccinated cohort compared with the pre-vaccine availability and unvaccinated cohorts: aHRs for depression and serious mental illness during weeks 1 through 4 after COVID-19 were 1.93 (95% CI, 1.88-1.98) and 1.49 (95% CI, 1.41-1.57) in the pre-vaccine availability cohort and 1.79 (95% CI, 1.68-1.90) and 1.45 (95% CI, 1.27-1.65) in the unvaccinated cohort compared with 1.16 (95% CI, 1.12-1.20) and 0.91 (95% CI, 0.85-0.98) in the vaccinated cohort. Elevation in incidence was higher and persisted longer after hospitalization for COVID-19. CONCLUSIONS AND RELEVANCE: In this study, incidence of mental illnesses was elevated for up to a year following severe COVID-19 in unvaccinated people. These findings suggest that vaccination may mitigate the adverse effects of COVID-19 on mental health