Integrons are genetic elements that are common in bacteria and are hotspots for genome evolution. They facilitate the acquisition and reassembly of gene cassettes encoding a variety of functions, including drug resistance. Despite their importance in clinical settings, the selective forces responsible for the evolution and maintenance of integrons are poorly understood. We present a mathematical model of integron evolution within bacterial populations subject to fluctuating antibiotic exposures. Bacteria carrying a functional integrase that mediates reshuffling of cassette genes and thereby modulates gene expression patterns compete with bacteria without a functional integrase. Our results indicate that for a wide range of parameters, the functional integrase can be stably maintained in the population despite substantial fitness costs. This selective advantage arises because gene-cassette shuffling generates genetic diversity, thus enabling the population to respond rapidly to changing selective pressures. We also show that horizontal gene transfer promotes stable maintenance of the integrase and can also lead to de novo assembly of integrons. Our model generates testable predictions for integron evolution, including loss of functional integrases in stable environments and selection for intermediate gene-shuffling rates in changing environments. Our results highlight the need for experimental studies of integron population biology.