Understanding the intricate relationship between marine geography and microbial functions is crucial for marine conservation and management. In this study, we conducted a comprehensive analysis of bacterial composition and function in nearshore and offshore sediments of the Beibu Gulf using 16S rRNA high-throughput sequencing. The results showed that Proteobacteria (average relative abundance: 27.07 %) and Desulfobacterota (average relative abundance: 12.28 %) were the most dominant phyla across all stations, while Woeseiaceae (3.26 %–8.31 %) and Anaerolineaceae (0.61 %–7.43 %) could serve as potential indicator species for pollution. In our study area, the α-diversity of bacterial communities in sediment samples showed an initial increase from coastal to offshore regions, followed by a decrease with further distance from the coastlines. The composition of sediment bacterial communities was mainly influenced by total phosphorus (R2 = 0.183, p < 0.01) and salinity (R2 = 0.550, p < 0.01). Furthermore, the sulfur (S) cycling genes of KEGG pathways displayed significant variations with the distance from shore, implying that S oxidation dominated in nearshore sediments, while S reduction occurred mainly in offshore sediments, which was attributed to the differences in redox conditions across diverse marine environments. These findings will not only enhance our current understanding of the intricate relationship between marine geography and microbial functions but also contribute to elucidating the biogeochemical characteristics of the Beibu Gulf. This research will provide valuable information and a solid scientific basis for the conservation and management of various marine areas.