Identification of the causal genes that control complex trait variation remains challenging, limiting our appreciation of the evolutionary processes that influence polymorphisms in nature. We cloned a quantitative trait locus that controls plant defensive chemistry, damage by insect herbivores, survival, and reproduction in the natural environments where this polymorphism evolved. These ecological effects are driven by duplications in the BCMA (branched-chain methionine allocation) loci controlling this variation and by two selectively favored amino acid changes in the glucosinolate-biosynthetic cytochrome P450 proteins that they encode. These changes cause a gain of novel enzyme function, modulated by allelic differences in catalytic rate and gene copy number. Ecological interactions in diverse environments likely contribute to the widespread polymorphism of this biochemical function.