The emergence and subsequent diversification of morphological novelties is a major feature of animal evolution1–9. However, in most cases little is known about the molecular basis of the evolution of novel structures and the genetic mechanisms underlying their diversification. The epandrial posterior lobes of the male genital arch is a novelty of some species of the Drosophila melanogaster subgroup10–13. The posterior lobes grasp the ovipositor of the female and then integrate between her abdominal tergites, and therefore these structures are important for copulation and species-recognition10–12,14–17. The posterior lobes evolved from co-option of a Hox regulated gene network from the posterior spiracles10and have since diversified in shape and size in the D. simulans clade in particular over the last 240,000 years driven by sexual selection18–21. The genetic basis of this diversification is highly polygenic but to the best of our knowledge none of the causative genes have yet been identified despite extensive mapping22–30. Identifying the genes underlying the diversification of these secondary sexual structures is essential to understanding the basis of changes in their morphology and the evolutionary impact on copulation and species recognition. Here, we show that the transcription factor encoded by Sox21b negatively regulates posterior lobe size during development. This is consistent with higher and expanded expression of Sox21b in D. mauritiana, which develops smaller posterior lobes compared to D. simulans. We tested this by generating reciprocal hemizygotes and confirmed that changes in Sox21b underlie posterior lobe evolution between these two species. Furthermore, we found that differences in posterior lobe size caused by the species-specific allele of Sox21b significantly affect the duration of copulation. Taken together, our study reveals the genetic basis for the sexual selection driven diversification of a novel morphological structure and its functional impact on copulatory behaviour.