The gut microbiota influences the phenotype and fitness of a host; however, limited information is currently available on the diversity and functions of the gut microbiota in wild animals. Therefore, we herein examined the diversity, composition, and potential functions of the gut microbiota in three Sceloporus lizards: Sceloporus aeneus, S. bicanthalis, and S. grammicus, inhabiting different habitats in a mountainous ecosystem. The gut bacterial community of S. bicanthalis from alpine grasslands at 4,150 m a.s.l. exhibited greater taxonomic, phylogenetic, and functional alpha diversities than its sister species S. aeneus from cornfields and human-induced grasslands at 2,600 m a.s.l. Bacteria of the genus Blautia and metabolic functions related to the degradation of aromatic compounds were more abundant in S. bicanthalis than in S. aeneus, whereas Oscillibacter and predicted functions related to amino acid metabolism and fermentation were more abundant in S. aeneus. The structure of the dominant and most prevalent bacteria, i.e., the core microbiota, was similar between the sister species from different habitats, but differed between S. grammicus and S. aeneus cohabiting at 2,600 m a.s.l. and between S. grammicus and S. bicanthalis cohabiting at 4,150 m a.s.l. These results suggest that phylogenetic relatedness defines the core microbiota, while the transient, i.e., non-core, microbiota is influenced by environmental differences in the habitats. Our comparisons between phylogenetically close species provide further evidence for the specialized and complex associations between hosts and the gut microbiota as well as insights into the roles of phylogeny and ecological factors as drivers of the gut microbiota in wild vertebrates.