The class II genes of the major histocompatibility complex encode proteins which play a crucial role in antigen presentation. They are among the most polymorphic proteins known, and this polymorphism is thought to be the result of natural selection. To understand the selective pressure acting on the protein and to examine possible differences in the evolutionary dynamics among species, we apply maximum likelihood models of codon substitution to analyze the DRB genes of six mammalian species: human, chimpanzee, macaque, tamarin, dog, and cow. The models account for variable selective pressures across codons in the gene and have the power to detect amino acid residues under either positive or negative selection. Our analysis detected positive selection in the DRB genes in each of the six mammals examined. Comparison with structural data reveals that almost all amino acid residues inferred to be under positive selection in humans are in the peptide binding region (PBR) and are in contact with the antigen side chains, although residues outside of but close to the PBR are also detected. Strong purifying selection is also detected in the PBR, at sites which contact the antigen and at sites which may be involved in dimerization or T cell binding. The analysis demonstrates the utility of the random-sites analysis even when structural information is available. The different mammalian species are found to share many positively or negatively selected sites, suggesting that their functional roles have remained very similar in the different species, despite the different habitats and pathogens of the species.