IMPORTANCE: People with psychosis are more likely to be born and live in densely populated and socioeconomically deprived environments, but it is unclear whether these associations are a cause or consequence of disorder. OBJECTIVE: To investigate whether trajectories of exposure to deprivation and population density before and after diagnosis are associated with psychotic disorders or nonpsychotic bipolar disorder. DESIGN, SETTING, AND PARTICIPANTS: This nested case-control study included all individuals born in Sweden between January 1, 1982, and December 31, 2001, diagnosed for the first time with an International Statistical Classification of Diseases and Related Health Problems, Tenth Revision (ICD-10) psychotic disorder or nonpsychotic bipolar disorder between their 15th birthday and cohort exit (December 31, 2016). One sex- and birth year-matched control participant per case was selected. Data analysis was performed from July 2021 to June 2023. EXPOSURES: The main exposures were quintiles of neighborhood-level deprivation and population density each year from birth to age 14 years and from first diagnosis until cohort exit. MAIN OUTCOMES AND MEASURES: The main outcomes were the odds of a serious mental illness outcome associated with trajectories of deprivation and population density, before and after diagnosis in cases. Group-based trajectory modeling was used to derive trajectories of each exposure in each period. Logistic regression was used to examine associations with outcomes. RESULTS: A total of 53 458 individuals (median [IQR] age at diagnosis in case patients, 23.2 [15.0-34.8] years; 30 746 [57.5%] female), including 26 729 case patients and 26 729 control participants, were studied. From birth to early adolescence, gradients were observed in exposure to deprivation and population density trajectories during upbringing and psychotic disorder, with those in the most vs least deprived (adjusted odds ratio [AOR], 1.17; 95% CI, 1.08-1.28) and most vs least densely populated (AOR, 1.49; 95% CI, 1.34-1.66) trajectories at greatest risk. A strong upward mobility trajectory to less deprived neighborhoods was associated with similar risk to living in the least deprived trajectory (AOR, 1.01; 95% CI, 0.91-1.12). Only 543 case patients (2.0%) drifted into more deprived areas after diagnosis; people with psychotic disorder were more likely to belong to this trajectory (AOR, 1.38; 95% CI, 1.16-1.65) or remain in the most deprived trajectory (AOR, 1.36; 95% CI, 1.24-1.48) relative to controls. Patterns were similar for nonpsychotic bipolar disorder and deprivation but weaker for population density. CONCLUSIONS AND RELEVANCE: In this case-control study, greater exposure to deprivation during upbringing was associated with increased risk of serious mental illness, but upward mobility mitigated this association. People with serious mental illness disproportionately remained living in more deprived areas after diagnosis, highlighting issues of social immobility. Prevention and treatment should be proportionately located in deprived areas according to need