Bacterial symbionts, with their shorter generation times and capacity for horizontal gene transfer (HGT), play a critical role in allowing marine organisms to cope with environmental change. The closure of the Isthmus of Panama created distinct environmental conditions in the Tropical Eastern Pacific (TEP) and Caribbean, offering a "natural experiment" for studying how closely related animals evolve and adapt under environmental change. However, the role of bacterial symbionts in this process is often overlooked. We sequenced the genomes of endosymbiotic bacteria in two sets of sister species of chemosymbiotic bivalves from the genera Codakia and Ctena (family Lucinidae) collected on either side of the Isthmus, to investigate how differing environmental conditions have influenced the selection of symbionts and their metabolic capabilities. The lucinid sister species hosted different Candidatus Thiodiazotropha symbionts and only those from the Caribbean had the genetic potential for nitrogen fixation, while those from the TEP did not. Interestingly, this nitrogen-fixing ability did not correspond to symbiont phylogeny, suggesting convergent evolution of nitrogen fixation potential under nutrient-poor conditions. Reconstructing the evolutionary history of the nifHDKT operon by including other lucinid symbiont genomes from around the world further revealed that the last common ancestor (LCA) of Ca. Thiodiazotropha lacked nif genes, and populations in oligotrophic habitats later re-acquired the nif operon through HGT from the Sedimenticola symbiont lineage. Our study suggests that HGT of the nif operon has facilitated niche diversification of the globally distributed Ca. Thiodiazotropha endolucinida species clade. It highlights the importance of nitrogen availability in driving the ecological diversification of chemosynthetic symbiont species and the role that bacterial symbionts may play in the adaptation of marine organisms to changing environmental conditions.