Jupiter and Saturn formed in a few million years (ref. 1) from a gas-dominated protoplanetary disk, and were susceptible to gas-driven migration of their orbits on timescales of only ∼100,000 years (ref. 2). Hydrodynamic simulations show that these giant planets can undergo a two-stage, inward-then-outward, migration. The terrestrial planets finished accreting much later, and their characteristics, including Mars' small mass, are best reproduced by starting from a planetesimal disk with an outer edge at about one astronomical unit from the Sun (1 au is the Earth-Sun distance). Here we report simulations of the early Solar System that show how the inward migration of Jupiter to 1.5 au, and its subsequent outward migration, lead to a planetesimal disk truncated at 1 au; the terrestrial planets then form from this disk over the next 30-50 million years, with an Earth/Mars mass ratio consistent with observations. Scattering by Jupiter initially empties but then repopulates the asteroid belt, with inner-belt bodies originating between 1 and 3 au and outer-belt bodies originating between and beyond the giant planets. This explains the significant compositional differences across the asteroid belt. The key aspect missing from previous models of terrestrial planet formation is the substantial radial migration of the giant planets, which suggests that their behaviour is more similar to that inferred for extrasolar planets than previously thought.