Postharvest decay of goji berries, mainly caused by Alternaria alternata, results in significant economic losses. To investigate the effects of melatonin (MLT) on resistance to Alternaria rot in goji berries, the fruits were immersed in the MLT solutions with varying concentrations (0, 25, 50, and 75 μmol L−1) and then inoculated with A. alternata. The results showed that the fruits treated with 50 μmol L−1 MLT exhibited the lowest disease incidence and least lesion diameter. Meanwhile, endogenous MLT in the fruits treated with 50 μmol L−1 MLT showed higher levels than in the control fruits during storage at 4 ± 0.5 °C. Further, the enzymatic activities and expressions of genes encoding peroxidase, phenylalanine ammonia-lyase, cinnamate 4-hydroxylase, 4-coumarate-CoA ligase, chalcone synthase, chalcone isomerase, and cinnamyl alcohol dehydrogenase were induced in the treated fruit during storage. UPLC-ESI-MS/MS revealed that secondary metabolites in the fruits on day 0, in order of highest to lowest levels, were rutin, p-coumaric acid, chlorogenic acid, ferulic acid, caffeic acid, naringenin, quercetin, kaempferol, and protocatechuic acid. MLT-treated fruits exhibited higher levels of secondary metabolites than the control. In conclusion, MLT treatment contributed to controlling the postharvest decay of goji fruit during storage by boosting endogenous MLT levels, thus activating the antioxidant system and secondary metabolism.