Ancient Rome was the capital of an empire of ~70 million inhabitants, but little is known about the genetics of ancient Romans. Here we present 127 genomes from 29 archaeological sites in and around Rome, spanning the past 12,000 years. We observe two major prehistoric ancestry transitions: one with the introduction of farming and another prior to the Iron Age. By the founding of Rome, the genetic composition of the region approximated that of modern Mediterranean populations. During the Imperial period, Rome’s population received net immigration from the Near East, followed by an increase in genetic contributions from Europe. These ancestry shifts mirrored the geopolitical affiliations of Rome and were accompanied by marked interindividual diversity, reflecting gene flow from across the Mediterranean, Europe, and North Africa.