Elucidating elementary mechanisms that underlie bacterial diversity is central to ecology1,2 and microbiome research3. Bacteria are known to coexist by metabolic specialization4, cooperation5 and cyclic warfare6,7,8. Many species are also motile9, which is studied in terms of mechanism10,11, benefit12,13, strategy14,15, evolution16,17 and ecology18,19. Indeed, bacteria often compete for nutrient patches that become available periodically or by random disturbances2,20,21. However, the role of bacterial motility in coexistence remains unexplored experimentally. Here we show that—for mixed bacterial populations that colonize nutrient patches—either population outcompetes the other when low in relative abundance. This inversion of the competitive hierarchy is caused by active segregation and spatial exclusion within the patch: a small fast-moving population can outcompete a large fast-growing population by impeding its migration into the patch, while a small fast-growing population can outcompete a large fast-moving population by expelling it from the initial contact area. The resulting spatial segregation is lost for weak growth–migration trade-offs and a lack of virgin space, but is robust to population ratio, density and chemotactic ability, and is observed in both laboratory and wild strains. These findings show that motility differences and their trade-offs with growth are sufficient to promote diversity, and suggest previously undescribed roles for motility in niche formation and collective expulsion–containment strategies beyond individual search and survival.