Cobalamin (B12), an essential nutrient and growth cofactor for many living organisms on Earth, can be fully synthesized only by selected prokaryotes in nature. Therefore, microbial communities related to B12 biosynthesis could serve as an example subsystem to disentangle the underlying ecological mechanisms balancing the function and taxonomic make-up of complex functional assemblages. By anchoring microbial traits potentially involved in B12 biosynthesis, we depict the biogeographic patterns of B12 biosynthesis genes and the taxa harboring them in the global ocean, despite the limitations of detecting de novo B12 synthesizers via metagenomes alone. Both the taxonomic and functional composition of B12 biosynthesis genes were strongly shaped by depth, differentiating the epipelagic zones from the mesopelagic layers. Functional genes related to B12 biosynthesis were relatively stably distributed across different oceans, but the taxa harboring them varied considerably, showing clear functional redundancy among microbial systems. Microbial taxa carrying B12 biosynthesis genes in the surface water were influenced by environmental factors such as temperature, oxygen, and nitrate. However, the composition of functional genes was only weakly associated with these environmental factors. Null model analyses demonstrated that determinism governed the variations in B12 biosynthesis genes, whereas a higher degree of stochasticity was associated with taxonomic variations. Significant associations were observed between the chlorophyll a concentration and B12 biosynthesis, confirming its importance in primary production in the global ocean. The results of this study reveal an essential ecological mechanism governing the assembly of microbes in nature: the environment selects for function rather than taxonomy; functional redundancy underlies stochastic community assembly.