Mangrove roots harbor a repertoire of microbial taxa that contribute to important ecological functions in mangrove ecosystems. However, the diversity, function, and assembly of mangrove root-associated microbial communities along a continuous fine-scale niche remain elusive. Here, we applied amplicon and metagenome sequencing to investigate the bacterial and fungal communities among four compartments (nonrhizosphere, rhizosphere, episphere, and endosphere) of mangrove roots. We found different distribution patterns for both bacterial and fungal communities in all four root compartments, which could be largely due to niche differentiation along the root compartments and exudation effects of mangrove roots. The functional pattern for bacterial and fungal communities was also divergent within the compartments. The endosphere harbored more genes involved in carbohydrate metabolism, lipid transport, and methane production, and fewer genes were found to be involved in sulfur reduction compared to other compartments. The dynamics of root-associated microbial communities revealed that 56–74% of endosphere bacterial taxa were derived from nonrhizosphere, whereas no fungal OTUs of nonrhizosphere were detected in the endosphere. This indicates that roots may play a more strictly selective role in the assembly of the fungal community compared to the endosphere bacterial community, which is consistent with the projections established in an amplification-selection model. This study reveals the divergence in the diversity and function of root-associated microbial communities along a continuous fine-scale niche, thereby highlighting a strictly selective role of soil-root interfaces in shaping the fungal community structure in the mangrove root systems.