Recent measurements by the Pierre Auger Observatory suggest that the composition of ultrahigh energy cosmic rays (UHECRs) becomes dominated by heavy nuclei at high energies. However, until now there has been no astrophysical motivation for considering a source highly enriched in heavy elements. Here we demonstrate that the outflows from gamma-ray bursts (GRBs) may indeed be composed primarily of nuclei with masses A∼ 40–200, which are synthesized as hot material expands away from the central engine. In particular, if the jet is magnetically dominated (rather than a thermally driven fireball) its low entropy enables heavy elements to form efficiently. Adopting the millisecond protomagnetar model for the GRB central engine, we show that heavy nuclei both are synthesized in protomagnetar winds and can in principle be accelerated to energies ≳1020 eV in the shocks or regions of magnetic reconnection that are responsible for powering the GRB. Similar results may apply to accretion-powered GRB models if the jet originates from a magnetized disc wind. Depending on the precise distribution of nuclei synthesized, we predict that the average primary mass may continue to increase beyond Fe group elements at the highest energies, possibly reaching the A≈ 90 (zirconium), A≈ 130 (tellurium) or even A≈ 195 (platinum) peaks. Future measurements of the UHECR composition at energies ≳1020 eV can thus confirm or constrain our model and, potentially, probe the nature of GRB outflows. The longer attenuation length of ultra-heavy nuclei through the extragalactic background light greatly expands the volume of accessible sources and alleviates the energetic constraints on GRBs as the source of UHECRs.