I found this after a brief Google search, so I might look into the topic more in the future. It seems pretty complicated, yet interesting.
Vitamin B12 synthesis by human small intestinal bacteria
In man, physiological amounts of vitamin B12 (cyanocobalamin) are absorbed by the intrinsic factor mediated mechanism…
“In man, physiological amounts of vitamin B12 (cyanocobalamin) are absorbed by the intrinsic factor mediated mechanism exclusively in the ileum. Human faeces contain appreciable quantities of vitamin B12 or vitamin B12-like material presumably produced by bacteria in the colon, but this is unavailable to the non-coprophagic individual. However, the human small intestine also often harbours a considerable microflora and this is even more extensive in apparently healthy southern Indian subjects. We now show that at least two groups of organisms in the small bowel, Pseudomonas and Klebsiella sp., may synthesise significant amounts of the vitamin.”