Background The degree to which infection with SARS-CoV-2 confers protection towards subsequent reinfection is not well described. In 2020, as part of Denmark's extensive, free-of-charge PCR-testing strategy, approximately 4 million individuals (69% of the population) underwent 10•6 million tests. Using these national PCR-test data from 2020, we estimated protection towards repeat infection with SARS-CoV-2. Methods In this population-level observational study, we collected individual-level data on patients who had been tested in Denmark in 2020 from the Danish Microbiology Database and analysed infection rates during the second surge of the COVID-19 epidemic, from Sept 1 to Dec 31, 2020, by comparison of infection rates between individuals with positive and negative PCR tests during the first surge (March to May, 2020). For the main analysis, we excluded people who tested positive for the first time between the two surges and those who died before the second surge. We did an alternative cohort analysis, in which we compared infection rates throughout the year between those with and without a previous confirmed infection at least 3 months earlier, irrespective of date. We also investigated whether differences were found by age group, sex, and time since infection in the alternative cohort analysis. We calculated rate ratios (RRs) adjusted for potential confounders and estimated protection against repeat infection as 1-RR. Findings During the first surge (ie, before June, 2020), 533 381 people were tested, of whom 11 727 (2•20%) were PCR positive, and 525 339 were eligible for follow-up in the second surge, of whom 11 068 (2•11%) had tested positive during the first surge. Among eligible PCR-positive individuals from the first surge of the epidemic, 72 (0•65% [95% CI 0•51-0•82]) tested positive again during the second surge compared with 16 819 (3•27% [3•22-3•32]) of 514 271 who tested negative during the first surge (adjusted RR 0•195 [95% CI 0•155-0•246]). Protection against repeat infection was 80•5% (95% CI 75•4-84•5). The alternative cohort analysis gave similar estimates (adjusted RR 0•212 [0•179-0•251], estimated protection 78•8% [74•9-82•1]). In the alternative cohort analysis, among those aged 65 years and older, observed protection against repeat infection was 47•1% (95% CI 24•7-62•8). We found no difference in estimated protection against repeat infection by sex (male 78•4% [72•1-83•2] vs female 79•1% [73•9-83•3]) or evidence of waning protection over time (3-6 months of follow-up 79•3% [74•4-83•3] vs ≥7 months of follow-up 77•7% [70•9-82•9]). Interpretation Our findings could inform decisions on which groups should be vaccinated and advocate for vaccination of previously infected individuals because natural protection, especially among older people, cannot be relied on.