Many biological functions are leaky, and organisms that perform them contribute some of their products to a community “marketplace” in which nonperforming individuals may compete for them. Leaky functions are partitioned unequally in microbial communities, and the evolutionary forces determining which species perform them and which become beneficiaries are poorly understood. Here, we demonstrate that the market principle of comparative advantage determines the distribution of a leaky antibiotic resistance gene in an environment occupied by two “species”—strains of Escherichia coli growing on mutually exclusive resources and thus occupying separate niches. Communities comprised of antibiotic-resistant cells were rapidly invaded by sensitive cells of both types. While the two phenotypes coexisted stably for 500 generations, in 15/18 replicates, antibiotic sensitivity became fixed in one species. Fixation always occurred in the same species despite both species being genetically identical except for their niche-defining mutation. In the absence of antibiotic, the fitness cost of resistance was identical in both species. However, the intrinsic resistance of the species that ultimately became the sole helper was significantly lower, and thus its reward for expressing the resistance gene was higher. Opportunity cost of resistance, not absolute cost or efficiency of antibiotic removal, determined which species became the helper, consistent with the economic theory of comparative advantage. We present a model that suggests that this market-like dynamic is a general property of Black Queen systems and, in communities dependent on multiple leaky functions, could lead to the spontaneous development of an equitable and efficient division of labor.