The Juno mission1 has provided an accurate determination of Jupiter’s gravitational field2, which has been used to obtain information about the planet’s composition and internal structure. Several models of Jupiter’s structure that fit the probe’s data suggest that the planet has a diluted core, with a total heavy-element mass ranging from ten to a few tens of Earth masses (about 5 to 15 per cent of the Jovian mass), and that heavy elements (elements other than hydrogen and helium) are distributed within a region extending to nearly half of Jupiter’s radius3,4. Planet-formation models indicate that most heavy elements are accreted during the early stages of a planet's formation to create a relatively compact core5,6,7 and that almost no solids are accreted during subsequent runaway gas accretion8,9,10. Jupiter’s diluted core, combined with its possible high heavy-element enrichment, thus challenges standard planet-formation theory. A possible explanation is erosion of the initially compact heavy-element core, but the efficiency of such erosion is uncertain and depends on both the immiscibility of heavy materials in metallic hydrogen and on convective mixing as the planet evolves11,12. Another mechanism that can explain this structure is planetesimal enrichment and vaporization13,14,15 during the formation process, although relevant models typically cannot produce an extended diluted core. Here we show that a sufficiently energetic head-on collision (giant impact) between a large planetary embryo and the proto-Jupiter could have shattered its primordial compact core and mixed the heavy elements with the inner envelope. Models of such a scenario lead to an internal structure that is consistent with a diluted core, persisting over billions of years. We suggest that collisions were common in the young Solar system and that a similar event may have also occurred for Saturn, contributing to the structural differences between Jupiter and Saturn16,17,18.