The major histocompatibility complex (MHC) harbours some of the most polymorphic loci in vertebrate genomes. MHC genes are thought to be subject to some form of balancing selection, most likely pathogen-mediated selection. Hence, MHC genes are excellent candidates for exploring adaptive processes. In this study, we investigated the genetic variation at exon 2 of the DRB class II MHC locus in 191 alpine chamois (Rupicapra rupicapra) from 10 populations in the eastern Alps of Italy. In particular, we were interested in distinguishing and estimating the relative impact of selective and demographic factors, while taking into account the confounding effect of recombination. The extremely high dn/ds ratio and the presence of trans-species polymorphisms suggest that a strong long-term balancing selection effect has been operating at this locus throughout the evolutionary history of this species. We analysed patterns of genetic variation within and between populations, and the mitochondrial D-loop polymorphism patterns were analysed to provide a baseline indicator of the effects of demographic processes. These analyses showed that (i) the chamois experienced a demographic decline in the last 5000–30 000 years, most likely related to the postglacial elevation in temperature; (ii) this demographic process can explain the results of neutrality tests applied to MHC variation within populations, but cannot justify the much weaker divergence between populations implied by MHC as opposed to mitochondrial DNA; (iii) similar sets of divergent alleles are probably maintained with similar frequencies by balancing selection in different populations, and this mechanism is also operating in small isolated populations, which are strongly affected by drift.