Although myocarditis and pericarditis were not observed as adverse events in coronavirus disease 2019 (COVID-19) vaccine trials, there have been numerous reports of suspected cases following vaccination in the general population. We undertook a self-controlled case series study of people aged 16 or older vaccinated for COVID-19 in England between 1 December 2020 and 24 August 2021 to investigate hospital admission or death from myocarditis, pericarditis and cardiac arrhythmias in the 1-28 days following adenovirus (ChAdOx1, n = 20,615,911) or messenger RNA-based (BNT162b2, n = 16,993,389; mRNA-1273, n = 1,006,191) vaccines or a severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2) positive test (n = 3,028,867). We found increased risks of myocarditis associated with the first dose of ChAdOx1 and BNT162b2 vaccines and the first and second doses of the mRNA-1273 vaccine over the 1-28 days postvaccination period, and after a SARS-CoV-2 positive test. We estimated an extra two (95% confidence interval (CI) 0, 3), one (95% CI 0, 2) and six (95% CI 2, 8) myocarditis events per 1 million people vaccinated with ChAdOx1, BNT162b2 and mRNA-1273, respectively, in the 28 days following a first dose and an extra ten (95% CI 7, 11) myocarditis events per 1 million vaccinated in the 28 days after a second dose of mRNA-1273. This compares with an extra 40 (95% CI 38, 41) myocarditis events per 1 million patients in the 28 days following a SARS-CoV-2 positive test. We also observed increased risks of pericarditis and cardiac arrhythmias following a positive SARS-CoV-2 test. Similar associations were not observed with any of the COVID-19 vaccines, apart from an increased risk of arrhythmia following a second dose of mRNA-1273. Subgroup analyses by age showed the increased risk of myocarditis associated with the two mRNA vaccines was present only in those younger than 40.