Importance Findings suggest that infections might be linked to the development of psychiatric disorders and suicidal behavior. Large-scale studies are needed to investigate the effect of infection on the risk of suicide. Objective To estimate the association between hospitalization with infection and the risk of death by suicide. Design, Setting, and Participants Nationwide, population-based, prospective cohort study with more than 149 million person-years of follow-up. Data were analyzed with survival analysis techniques and were adjusted for sex, age, calendar period, cohabitation status, socioeconomic status, and the Charlson Comorbidity Index. Individual data were drawn from Danish longitudinal registers. A total of 7.22 million individuals 15 years or older living in Denmark between January 1, 1980, and December 31, 2011, were observed during a 32-year follow-up period. Main Outcomes and Measures The risk of death by suicide was identified in the Danish Cause of Death Register. Incidence rate ratios (IRRs) and accompanying 95% CIs were used as measures of relative risk. Results In 7 221 578 individuals (3 601 653 men and 3 619 925 women) observed for a total of 149 061 786 person-years, 32 683 suicides were observed during the follow-up period. Among the suicides, 7892 (24.1%) individuals had previously been diagnosed as having an infection during a hospitalization. Hospitalization with infection was linked to an elevated risk of suicide, with an IRR of 1.42 (95% CI, 1.38-1.46) compared with those without prior infection. Dose-response relationships were observed with respect to the number of hospital contacts for different infections. For example, having 7 or more infections was linked to an IRR of 2.90 (95% CI, 2.14-3.93). The number of days of treatment for infections was associated with an elevated risk of suicide in a dose-response relationship. More than 3 months of hospital treatment was linked to an IRR of 2.38 (95% CI, 2.05-2.76). The population-attributable risk associated with hospitalization with infection accounted for 10.1% of suicides. Conclusions and Relevance An increased risk of death by suicide was found among individuals hospitalized with infection in prospective and dose-response relationships. These findings indicate that infections may have a relevant role in the pathophysiological mechanisms of suicidal behavior.