The standard model for giant planet formation is based on the accretion of solids by a growing planetary embryo, followed by rapid gas accretion once the planet exceeds a so-called critical mass. However, the dominant size of the accreted solids (‘pebbles’ of the order of centimetres or ‘planetesimals’ of the order of kilometres to hundreds of kilometres) is unknown. Recently, high-precision measurements of isotopes in meteorites have provided evidence for the existence of two reservoirs of small bodies in the early Solar System. These reservoirs remained separated from ~1 Myr until ~3 Myr after the Solar System started to form. This separation is interpreted as resulting from Jupiter growing and becoming a barrier for material transport. In this framework, Jupiter reached ~20 Earth masses ($M_{\oplus}$) within ~1 Myr and slowly grew to ~50 $M_{\oplus}$ in the subsequent 2 Myr before reaching its present-day mass3. The evidence that Jupiter’s growth slowed after reaching 20 M⊕ for at least 2 Myr is puzzling because a planet of this mass is expected to trigger fast runaway gas accretion. Here, we use theoretical models to describe the conditions allowing for such a slow accretion and show that Jupiter grew in three distinct phases. First, rapid pebble accretion supplied the major part of Jupiter’s core mass. Second, slow planetesimal accretion provided the energy required to hinder runaway gas accretion during the 2 Myr. Third, runaway gas accretion proceeded. Both pebbles and planetesimals therefore play an important role in Jupiter’s formation.