Gut microbiota affect host health and behavior throughout life but their relationship in endangered animals is poorly understood. Malayan sun bears are protected as an endangered wild species. Abnormal behavior such as stereotypies can be used as welfare indicators of animals in zoos. In this study, the abundance and diversity of fecal microbiota from six Malayan sun bears in Tianjin Zoo were assessed using 16S rRNA Illumina technology, and stereotypies were analyzed to identify behavioral patterns in the bears, separated into two age groups. Stereotypic behavior did not differ significantly between bears of different ages, but bacterial diversity in bears from the elderly group was significantly higher than that of the adult group. The gut microbiota of Malayan sun bears was dominated by Firmicutes (54.70%), Proteobacteria (41.90%), and Fusobacteria (1.80%). Findings from this study facilitate understanding of the relationship between gut microbiota and behavioral patterns, which could benefit the welfare of animals in captivity.