Bacterial flagella at first sight appear uniquely sophisticated in structure, so much so that they have even been considered 'irreducibly complex' by the intelligent design movement. However, a more detailed analysis reveals that these remarkable pieces of molecular machinery are the product of processes that are fully compatible with Darwinian evolution. In this chapter we present evidence for such processes, based on a review of experimental studies, molecular phylogeny and microbial genomics. Several processes have played important roles in flagellar evolution: self-assembly of simple repeating subunits, gene duplication with subsequent divergence, recruitment of elements from other systems ('molecular bricolage'), and recombination. We also discuss additional tentative new assignments of homology (FliG with MgtE, FliO with YscJ). In conclusion, rather than providing evidence of intelligent design, flagellar and non-flagellar Type III secretion systems instead provide excellent case studies in the evolution of complex systems from simpler components.