Measures of exoplanet bulk densities indicate that small exoplanets with radius less than 3 Earth radii (R⊕) range from low-density sub-Neptunes containing volatile elements1 to higher-density rocky planets with Earth-like2 or iron-rich3 (Mercury-like) compositions. Such astonishing diversity in observed small exoplanet compositions may be the product of different initial conditions of the planet-formation process or different evolutionary paths that altered the planetary properties after formation4 . Planet evolution may be especially affected by either photoevaporative mass loss induced by high stellar X-ray and extreme ultraviolet (XUV) flux5 or giant impacts6 . Although there is some evidence for the former7,8 , there are no unambiguous findings so far about the occurrence of giant impacts in an exoplanet system. Here, we characterize the two innermost planets of the compact and near-resonant system Kepler-107 (ref.9 ). We show that they have nearly identical radii (about 1.5–1.6R⊕), but the outer planet Kepler-107 c is more than twice as dense (about 12.6 g cm–3 ) as the innermost Kepler-107 b (about 5.3 g cm−3 ). In consequence, Kepler-107 c must have a larger iron core fraction than Kepler-107 b. This imbalance cannot be explained by the stellar XUV irradiation, which would conversely make the more-irradiated and less-massive planet Kepler-107 b denser than Kepler-107 c. Instead, the dissimilar densities are consistent with a giant impact event on Kepler-107 c that would have stripped off part of its silicate mantle. This hypothesis is supported by theoretical predictions from collisional mantle stripping10 , which match the mass and radius of Kepler-107 c.