In northwestern Africa, lifestyle transitioned from foraging to food production around 7,400 years ago but what sparked that change remains unclear. Archaeological data support conflicting views: (1) that migrant European Neolithic farmers brought the new way of life to North Africa1–3or (2) that local hunter-gatherers adopted technological innovations4,5. The latter view is also supported by archaeogenetic data6. Here we fill key chronological and archaeogenetic gaps for the Maghreb, from Epipalaeolithic to Middle Neolithic, by sequencing the genomes of nine individuals (to between 45.8- and 0.2-fold genome coverage). Notably, we trace 8,000 years of population continuity and isolation from the Upper Palaeolithic, via the Epipaleolithic, to some Maghrebi Neolithic farming groups. However, remains from the earliest Neolithic contexts showed mostly European Neolithic ancestry. We suggest that farming was introduced by European migrants and was then rapidly adopted by local groups. During the Middle Neolithic a new ancestry from the Levant appears in the Maghreb, coinciding with the arrival of pastoralism in the region, and all three ancestries blend together during the Late Neolithic. Our results show ancestry shifts in the Neolithization of northwestern Africa that probably mirrored a heterogeneous economic and cultural landscape, in a more multifaceted process than observed in other regions.