Cyclophosphamide (CP) is an anticancer drug that adversely affects immunity and thymus structure. Melatonin is a hormone secreted by the pineal gland. It boosts immunity and has antioxidant properties. Therefore, the present study was conducted to investigate the possible protective effect of melatonin on CP-induced changes in the rat thymus. Forty male albino rats were used and divided equally into four main groups. Group I was the control group. Group II (melatonin group) received melatonin at a dose of 10 mg/kg body weight/day by intraperitoneal injection throughout the experimental period. Group III (CP group) received 200 mg/kg body weight CP by a single intraperitoneal injection. Group IV (CP + melatonin group) received melatonin intraperitoneally at a dose of 10 mg/kg body weight/day starting 5 days prior to CP injection until the end of the experiment. All rats were euthanized 7 days after CP injection. Administration of CP in group III resulted in depletion of the cortical thymoblasts. In addition, CD34-immunopositive stained stem cells decreased and mast cell infiltration increased. Electron microscopy showed degeneration of thymoblasts and vacuolization of epithelial reticular cells. Administration of melatonin with CP in group IV showed considerable protection of thymic histology. In conclusion, melatonin may protect against CP-induced thymic injury.Supplementary InformationThe online version contains supplementary material available at 10.1007/s00418-023-02185-6.