Ecological niche partitioning shapes microbial communities in terrestrial mosses, yet its underlying mechanisms and associated viral diversity remain poorly understood. Here, we characterized prokaryotic and viral communities in the rhizosphere soil (Rs) and endophytic niche (Pc) of Pogonatum cirratum using amplicon and metagenomic sequencing. Rs exhibited higher species richness, co-dominated by Pseudomonadota , Acidobacteriota , and Actinomycetota , whereas Pc was dominated by Pseudomonadota (81.13%) but showed greater functional diversity. Source tracking revealed that 10.57% of Pc taxa originated from Rs, suggesting host-mediated filtration of beneficial microbes. Deterministic processes predominantly governed prokaryotic assembly, with iron cycling accounting for ∼14% of total metabolic potential in both niches. Rs contained more biosynthetic gene clusters, while viral communities diverged in taxonomy and auxiliary metabolic genes profiles. These findings demonstrate that P. cirratum maintains compartmentalized prokaryotic and viral communities through niche-specific abiotic filtering and biotic selection, promoting nutrient acquisition and stress resilience in bryophyte-dominated ecosystems.