The production of elements by rapid neutron capture (r-process) in neutron-star mergers is expected theoretically and is supported by multimessenger observations of gravitational-wave event GW170817: this production route is in principle sufficient to account for most of the r-process elements in the Universe. Analysis of the kilonova that accompanied GW170817 identified delayed outflows from a remnant accretion disk formed around the newly born black hole as the dominant source of heavy r-process material from that event. Similar accretion disks are expected to form in collapsars (the supernova-triggering collapse of rapidly rotating massive stars), which have previously been speculated to produce r-process elements. Recent observations of stars rich in such elements in the dwarf galaxy Reticulum II, as well as the Galactic chemical enrichment of europium relative to iron over longer timescales, are more consistent with rare supernovae acting at low stellar metallicities than with neutron-star mergers. Here we report simulations that show that collapsar accretion disks yield sufficient r-process elements to explain observed abundances in the Universe. Although these supernovae are rarer than neutron-star mergers, the larger amount of material ejected per event compensates for the lower rate of occurrence. We calculate that collapsars may supply more than 80 per cent of the r-process content of the Universe.