Bone mesenchymal stem cells (BMSCs) play an important role in maintaining the dynamic balance of bone metabolism. Recent studies have reported that a decrease in the osteogenic function of MSCs is strongly associated with osteoporosis. Melatonin is a neuroendocrine hormone produced in the pineal gland and is essential in the physiological regulation. This study is aimed at exploring the effect of melatonin on MSCs osteoblastic differentiation and elucidate the underlying mechanisms. We isolated BMSCs from rat bone marrow and demonstrated that melatonin improved osteogenic differentiation of BMSCs by the alizarin red staining and ALP staining. We then showed that melatonin enhanced osteogenic gene expression in BMSCs, including ALP, Col 1, OCN, OPN, and RUNX2. We further revealed that melatonin inhibited the inflammatory response of BMSCs by suppressing the NF-κB signaling pathways. In light of this, we found that the NF-κB pathway-specific activator TNF-αactivated the NF-κB pathway, inhibited osteogenic differentiation, and induced inflammatory response in BMSCs. Melatonin was found to reverse the inhibitory effect of TNF-αon osteogenic differentiation and inflammation in BMSCs. Taken together, these findings indicated that melatonin may have therapeutic potential to be used for the treatment of osteoporosis.