Myocardial infarction- (MI-) induced myocardial damage is mainly attributed to the loss of cardiomyocytes. Pyroptosis is a newly recognized form of programmed cell necrosis that is associated with the progression of MI. Melatonin has been shown to exert cardioprotective effects against cardiac damage in multiple cardiovascular diseases. However, the effect of melatonin on pyroptosis-induced cardiac injury in MI has not been elucidated. Herein, we found that melatonin administration ameliorated cardiac dysfunction and reduced cardiomyocyte death both in mice following coronary artery ligation and in H9C2 cells exposed to hypoxia. The results also showed that pyroptosis was induced both in vivo and in vitro, as evidenced by increased NLRP3, cleaved caspase-1, GSDMD-N, and mature IL-1β and IL-18 levels, and these changes were decreased by melatonin treatment. Furthermore, we observed that TLR4 and NF-κB levels were increased by MI or hypoxia, and these increases were reversed by melatonin. The antipyroptotic action of melatonin was abrogated by treatment with an agonist of the TLR4/NF-κB signaling pathway. Our results indicate that melatonin can exert cardioprotective effects by inhibiting NLRP3 inflammasome-induced pyroptosis through modulation of the TLR4/NF-κB signaling pathway and provide strong evidence for the utility of melatonin in the treatment of MI.