Background The planting of mangroves is extensively used to control the invasive plant Spartina alterniflora in coastal wetlands. Different plant species release diverse sets of small organic compounds that affect rhizosphere conditions and support high levels of microbial activity. The root-associated microbial community is crucial for plant health and soil nutrient cycling, and for maintaining the stability of the wetland ecosystem. Methods High-throughput sequencing was used to assess the structure and function of the soil bacterial communities in mudflat soil and in the rhizosphere soils of S. alterniflora, mangroves, and native plants in the Oujiang estuarine wetland, China. A distance-based redundancy analysis (based on Bray–Curtis metrics) was used to identify key soil factors driving bacterial community structure. Results S. alterniflora invasion and subsequent mangrove afforestation led to the formation of distinct bacterial communities. The main soil factors driving the structure of bacterial communities were electrical conductivity (EC), available potassium (AK), available phosphorus (AP), and organic matter (OM). S. alterniflora obviously increased EC, OM, available nitrogen (AN), and NO3−-N contents, and consequently attracted copiotrophic Bacteroidates to conduct invasion in the coastal areas. Mangroves, especially Kandelia obovata, were suitable pioneer species for restoration and recruited beneficial Desulfobacterota and Bacilli to the rhizosphere. These conditions ultimately increased the contents of AP, available sulfur (AS), and AN in soil. The native plant species Carex scabrifolia and Suaeda glauca affected coastal saline soil primarily by decreasing the EC, rather than by increasing nutrient contents. The predicted functions of bacterial communities in rhizosphere soils were related to active catabolism, whereas those of the bacterial community in mudflat soil were related to synthesis and resistance to environmental factors. Conclusions Ecological restoration using K. obovata has effectively improved a degraded coastal wetland mainly through increasing phosphorus availability and promoting the succession of the microbial community.