Sexually dimorphic traits play key roles in animal evolution and behavior. Little is known, however, about the mechanisms governing their development and evolution. One recently evolved dimorphic trait is the male-specific abdominal pigmentation of Drosophila melanogaster, which is repressed in females by the Bric-à-brac (Bab) proteins. To understand the regulation and origin of this trait, we have identified and traced the evolution of the genetic switch controlling dimorphic bab expression. We show that the HOX protein Abdominal-B (ABD-B) and the sex-specific isoforms of Doublesex (DSX) directly regulate a bab cis-regulatory element (CRE). In females, ABD-B and DSX(F) activate bab expression whereas in males DSX(M) directly represses bab, which allows for pigmentation. A new domain of dimorphic bab expression evolved through multiple fine-scale changes within this CRE, whose ancestral role was to regulate other dimorphic features. These findings reveal how new dimorphic characters can emerge from genetic networks regulating pre-existing dimorphic traits.