This study is to investigate the relation between serum dehydroepiandrosterone (DHEA) and its sulfate (DHEAS) levels and the risk of osteoporosis in patients with T2DM. This cross-sectional study involved 938 hospitalized patients with T2DM. Linear regression models were used to explore the relationship between DHEA and DHEAS and the BMD at different skeletal sites. Multinominal logistic regression models and the restricted cubic spline (RCS) were used to evaluate the associations of DHEA and DHEAS with the risks of osteopenia and/or osteoporosis. In postmenopausal women with T2DM, after adjustment for confounders including testosterone and estradiol, DHEA showed a significant positive correlation with lumbar spine BMD (P = 0.013). Moreover, DHEAS exhibited significant positive correlations with BMD at three skeletal sites: including femoral neck, total hip, and lumbar spine (all P < 0.05). Low DHEA and DHEAS levels were associated with increased risk of osteopenia and/or osteoporosis (all P < 0.05) and the risk of osteoporosis gradually decreased with increasing DHEAS levels (P overall = 0.018, P-nonlinear = 0.559). However, DHEA and DHEAS levels in men over the age of 50 with T2DM were not associated with any of above outcomes. In patients with T2DM, independent of testosterone and estradiol, higher DHEA and DHEAS levels are associated with higher BMD and lower risk of osteopenia/osteoporosis in postmenopausal women but not men over the age of 50.