Introduction: Evidence has suggested an association between bacterial infection and increased risk of subsequent major mental disorders (MMDs). Whether such association varies with different pathogens remains unclear. We aimed to investigate the risk of subsequent MMDs after exposure to bacterial pathogens in children and adolescents. Methods: Between 1997 and 2012, we enrolled a nationwide cohort of 14,024 children and adolescents with hospitalized bacterial infection, and noninfected controls were 1:4 matched for demographics. There were 11 investigated pathogens, namely, Streptococcus, Staphylococcus, Pseudomonas, Klebsiella, Hemophilus, Mycoplasma, Tuberculosis, Meningococcus, Escherichia, Chlamydia, and Scrub typhus. The primary outcomes were the subsequent risk of seven MMDs, namely, autism spectrum disorder (ASD), attention-deficiency hyperactivity disorder (ADHD), obsessive-compulsive disorder (OCD), tic disorder, schizophrenia, bipolar disorder, and depressive disorder. The secondary outcomes were the subsequent risk of exposure to psychotropic medications. Results: Pooled bacterial infection was associated with increased risk of the six MMDs – ASD (reported as hazard ratios with 95% confidence intervals: 13.80; 7.40–25.75), ADHD (6.93; 5.98–8.03), OCD (3.93; 1.76–8.76), tic disorder (6.19; 4.44–8.64), bipolar disorder (2.50; 1.28–4.86), and depressive disorder (1.93; 1.48–2.51) – and exposure to four psychotropic medications, including ADHD drugs (11.81; 9.72–14.35), antidepressants (2.96; 2.45–3.57), mood stabilizers (4.51; 2.83–7.19), and atypical antipsychotics (4.23; 3.00–5.96) compared to controls. The associations among MMDs and specific pathogens varied. Importantly, Streptococcus was associated with the most MMDs (six MMDs), and ADHD was associated with eight bacterial pathogen infections. Conclusions: After bacterial infection, the risk of MMDs increased in children and adolescents compared to controls, and such associations varied with different pathogens. Future studies are warranted to validate our study findings and investigate the potential mechanisms.