Polymorphisms in the androgen receptor (AR) gene and genes encoding enzymes involved in synthesis of sex steroids (e.g., the CYP19 gene encoding aromatase) have recently received attention in osteoporosis research. In the Danish Osteoporosis Prevention Study, recent postmenopausal women were allocated to either hormone replacement therapy (HRT) or no treatment, We genotyped 1792 women for the CYP19 (TTTA)n repeat [short (TTTA) n ≤ 7 or long (TTTA)n > 7] the CYP19 C 1558-T, and the AR (CAG)n repeat polymorphism [short (CAG)n < 22, long (CAG)n > 22], and investigated associations with bone mineral density (BMD) and 5-year change in BMD. The CYP19 polymorphisms were in strong linkage disequilibrium. Perimenopausal bone mass or bone loss in untreated women was not associated with the CYP19 polymorphisms. In hormone-treated women, BMD increase in the femoral neck was highest (+0.3%/year) for long CYP19 alleles, lowest (-0.09%/year) for short alleles, and intermediate (-0.002%/year) in heterozygous women, P = 0.015. Differences were also significant in the lumbar spine, total hip, and ultradistal forearm. The C1558-T T-allele was associated with a more pronounced response to HRT (P = 0.04, total hip). AR genotype was not related to BMD, but a modifying effect of sex hormone-binding globulin (SHBG) was present. In the highest SHBG quartile (SHBG > 95 nmol/l, n = 222), AR genotype was associated with baseline BMD (femoral neck: P < 0.001, total hip: P = 0.008), but without a clear gene dosage effect. We have demonstrated that polymorphisms in the CYP19 gene are associated with the magnitude of bone gain in response to HRT and that the (CAG)n repeat polymorphism in the AR gene is associated with bone mass in women with high levels of SHBG. These findings emphasize the complexity of the genetics of bone mass and bone loss.