Circadian rhythm disruption (CRD) is highly prevalent in modern society and contributes to numerous disorders, including erectile dysfunction (ED). Melatonin (MT) possesses well‑established functions in regulating circadian rhythm and demonstrating antioxidant ability; however, whether MT could preserve CRD‑induced ED and the underlying mechanism has never been reported. A rat model with CRD‑induced ED was designed by changing light‑dark cycle (2h:2h alteration) and then intraperitoneally administering MT with low (5 mg/kg/day) and high (10 mg/kg/day) dosages. A total of 4 weeks later, rats' erectile function was measured and penile corpus cavernosum was subsequently harvested for analysis. In addition, bioinformatics analysis was performed to filter the possible molecular target, while lipopolysaccharide (LPS)‑treated human umbilical vein endothelial cells (HUVECs) were selected to imitate CRD stimulation in vivo to further verify the underlying molecular mechanism. CRD significantly reduced rats' maximal intracavernous pressure (mICP) and mICP/mean arterial pressure (MAP) ratio, it also inhibited endothelial nitric oxide synthase/nitric oxide/cyclic guanosine monophosphate concentrations and injured normal penile corpus cavernosum structure, suggesting rats' normal erectile function was impaired; however, this CRD‑induced ED was preserved by MT. Theandexperiments respectively proved that CRD increased oxidative stress of penile corpus cavernosum and HUVECs by reducing nuclear factor erythroid 2‑related factor 2 (Nrf2)/heme oxygenase‑1 (HO‑1) production, while MT increased Nrf2/HO‑1 to inhibit the oxidative stress. Meanwhile, CRD promoted pyroptosis in penile corpus cavernosum and HUVECs by increasing NLR family pyrin domain containing 3 (NLRP3) activation, which was relieved by MT through the attenuation of oxidative stress. Moreover, the reactive oxygen species inhibitor (NAC) inhibited CRD‑induced pyroptosis of HUVECs to preserve normal function, which confirmed that MT alleviated NLRP3‑mediated pyroptosis to preserved CRD‑induced ED by reducing oxidative stress. In conclusion, it was demonstrated that CRD‑induced ED by triggering an oxidative stress‑pyroptosis cascade. Conversely, MT treatment effectively counteracts this pathology by activating the Nrf2/HO‑1 pathway to suppress oxidative stress, thereby attenuating NLRP3‑mediated pyroptosis and ultimately restoring erectile function. These results provide the first systematic evidence for the central role of the oxidative stress‑pyroptosis axis in CRD‑induced ED, establishing a solid theoretical foundation for MT as a promising therapeutic strategy for CRD‑related ED. in vivo in vitro