Current theories of planetary evolution predict that infant giant planets have large radii and very low densities before they slowly contract to reach their final size after about several hundred million years 1, 2. These theoretical expectations remain untested to date, despite the increasing number of exoplanetary discoveries, as the detection and characterisation of very young planets is extremely challenging due to the intense stellar activity of their host stars 3, 4. However, the recent discoveries of young planetary transiting systems allow to place initial constraints on evolutionary models5–9. With an estimated age of 20 million years, V1298 Tau is one of the youngest solar-type stars known to host transiting planets: it harbours a multiple system composed of two Neptune-sized, one Saturn-sized, and one Jupiter-sized planets 10, 11. Here we report the dynamical masses of two of the four planets. We find that planet b, with an orbital period of 24 days, has a mass of 0.60 Jupiter masses and a density similar to the giant planets of the Solar System and other known giant exoplanets with significantly older ages 12, 13. Planet e, with an orbital period of 40 days, has a mass of 1.21 Jupiter masses and a density larger than most giant exoplanets. This is unexpected for planets at such a young age and suggests that some giant planets might evolve and contract faster than anticipated, thus challenging current models of planetary evolution.