The Juno mission has provided an accurate determination of Jupiters gravitational field, which has been used to obtain information about the planets composition and internal structure. Several models of Jupiters structure that fit the probes 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 (~5-15 % of the Jovian mass), and that heavy elements (elements other than H and He) are distributed within a region extending to nearly half of Jupiters radius. Planet-formation models indicate that most heavy elements are accreted during the early stages of a planets formation to create a relatively compact core and that almost no solids are accreted during subsequent runaway gas accretion. Jupiters 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 evolves. Another mechanism that can explain this structure is planetesimal enrichment and vaporization 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 Saturn.