BACKGROUND: COVID-19 is associated with increased risks of neurological and psychiatric sequelae in the weeks and months thereafter. How long these risks remain, whether they affect children and adults similarly, and whether SARS-CoV-2 variants differ in their risk profiles remains unclear. METHODS: In this analysis of 2-year retrospective cohort studies, we extracted data from the TriNetX electronic health records network, an international network of de-identified data from health-care records of approximately 89 million patients collected from hospital, primary care, and specialist providers (mostly from the USA, but also from Australia, the UK, Spain, Bulgaria, India, Malaysia, and Taiwan). A cohort of patients of any age with COVID-19 diagnosed between Jan 20, 2020, and April 13, 2022, was identified and propensity-score matched (1:1) to a contemporaneous cohort of patients with any other respiratory infection. Matching was done on the basis of demographic factors, risk factors for COVID-19 and severe COVID-19 illness, and vaccination status. Analyses were stratified by age group (age <18 years [children], 18-64 years [adults], and ≥65 years [older adults]) and date of diagnosis. We assessed the risks of 14 neurological and psychiatric diagnoses after SARS-CoV-2 infection and compared these risks with the matched comparator cohort. The 2-year risk trajectories were represented by time-varying hazard ratios (HRs) and summarised using the 6-month constant HRs (representing the risks in the earlier phase of follow-up, which have not yet been well characterised in children), the risk horizon for each outcome (ie, the time at which the HR returns to 1), and the time to equal incidence in the two cohorts. We also estimated how many people died after a neurological or psychiatric diagnosis during follow-up in each age group. Finally, we compared matched cohorts of patients diagnosed with COVID-19 directly before and after the emergence of the alpha (B.1.1.7), delta (B.1.617.2), and omicron (B.1.1.529) variants. FINDINGS: We identified 1 487 712 patients with a recorded diagnosis of COVID-19 during the study period, of whom 1 284 437 (185 748 children, 856 588 adults, and 242 101 older adults; overall mean age 42·5 years [SD 21·9]; 741 806 [57·8%] were female and 542 192 [42·2%] were male) were adequately matched with an equal number of patients with another respiratory infection. The risk trajectories of outcomes after SARS-CoV-2 infection in the whole cohort differed substantially. While most outcomes had HRs significantly greater than 1 after 6 months (with the exception of encephalitis; Guillain-Barré syndrome; nerve, nerve root, and plexus disorder; and parkinsonism), their risk horizons and time to equal incidence varied greatly. Risks of the common psychiatric disorders returned to baseline after 1-2 months (mood disorders at 43 days, anxiety disorders at 58 days) and subsequently reached an equal overall incidence to the matched comparison group (mood disorders at 457 days, anxiety disorders at 417 days). By contrast, risks of cognitive deficit (known as brain fog), dementia, psychotic disorders, and epilepsy or seizures were still increased at the end of the 2-year follow-up period. Post-COVID-19 risk trajectories differed in children compared with adults: in the 6 months after SARS-CoV-2 infection, children were not at an increased risk of mood (HR 1·02 [95% CI 0·94-1·10) or anxiety (1·00 [0·94-1·06]) disorders, but did have an increased risk of cognitive deficit, insomnia, intracranial haemorrhage, ischaemic stroke, nerve, nerve root, and plexus disorders, psychotic disorders, and epilepsy or seizures (HRs ranging from 1·20 [1·09-1·33] to 2·16 [1·46-3·19]). Unlike adults, cognitive deficit in children had a finite risk horizon (75 days) and a finite time to equal incidence (491 days). A sizeable proportion of older adults who received a neurological or psychiatric diagnosis, in either cohort, subsequently died, especially those diagnosed with dementia or epilepsy or seizures. Risk profiles were similar just before versus just after the emergence of the alpha variant (n=47 675 in each cohort). Just after (vs just before) the emergence of the delta variant (n=44 835 in each cohort), increased risks of ischaemic stroke, epilepsy or seizures, cognitive deficit, insomnia, and anxiety disorders were observed, compounded by an increased death rate. With omicron (n=39 845 in each cohort), there was a lower death rate than just before emergence of the variant, but the risks of neurological and psychiatric outcomes remained similar. INTERPRETATION: This analysis of 2-year retrospective cohort studies of individuals diagnosed with COVID-19 showed that the increased incidence of mood and anxiety disorders was transient, with no overall excess of these diagnoses compared with other respiratory infections. In contrast, the increased risk of psychotic disorder, cognitive deficit, dementia, and epilepsy or seizures persisted throughout. The differing trajectories suggest a different pathogenesis for these outcomes. Children have a more benign overall profile of psychiatric risk than do adults and older adults, but their sustained higher risk of some diagnoses is of concern. The fact that neurological and psychiatric outcomes were similar during the delta and omicron waves indicates that the burden on the health-care system might continue even with variants that are less severe in other respects. Our findings are relevant to understanding individual-level and population-level risks of neurological and psychiatric disorders after SARS-CoV-2 infection and can help inform our responses to them. FUNDING: National Institute for Health and Care Research Oxford Health Biomedical Research Centre, The Wolfson Foundation, and MQ Mental Health Research.