Phosphorus-solubilizing microorganisms convert insoluble phosphorus in the soil into phosphorus that can be absorbed by plants. Soluble phosphate combines with heavy metals to form precipitation, reducing the content of available heavy metals, thereby reducing the absorption of heavy metals by crops, which plays an important role in the remediation of heavy metal-contaminated soil. The effects of the immobilization of Cd and Pb and the release of PO43- by the phosphorus-solubilizing bacterium Klebsiella sp. M2 were studied through solution culture experiments. In addition, the effects of strain M2 on wheat uptake of Cd and Pb and its microbiological mechanism were also explored through pot experiments. The results showed that strain M2 reduced the concentrations of Cd and Pb and increased the concentration of PO43- in the solution through cell wall adsorption and induced phosphate precipitation. Pot experiments showed that compared to those in the CK group and inactivated strain M2 group, inoculation with live strain M2 significantly increased (123%-293%) the contents of Ca2-P and Ca8-P in rhizosphere soil, decreased the content of DTPA-Cd (34.48%) and DTPA-Pb (36.72%) in wheat rhizosphere soil, and thus hindered the accumulation of Cd and Pb in wheat grains. Moreover, high-throughput sequencing results showed that strain M2 significantly increased the diversity of wheat rhizosphere bacterial communities; increased the relative abundance of Proteobacteria, Gemmatimonadetes, and Bacteroidota in wheat rhizosphere soil; and increased the proportion of heavy metal-immobilizing and phosphorus-promoting bacteria in wheat rhizosphere soil (mainly Sphingomonas, Nocardioides, Bacillus, Gemmatimonas, and Enterobacter). These bacterial genera played an important role in immobilizing heavy metals and preventing wheat from absorbing heavy metals. These results provide bacterial resources and theoretical basis for the bioremediation of heavy metal-contaminated farmland.