Melatonin, a bioactive compound and an important signaling molecule produced in plants and animals, is involved in many biological processes. However, its function and synthetic pathways in fungi are poorly understood. Here, the samples from Tolypocladium guangdongense, a highly valued edible fungus with functional food properties, were collected under different experimental conditions to quantify the levels of melatonin and its intermediates. The results showed that the intracellular melatonin content was markedly improved by Congo red (CR), cold, and heat stresses; the levels of intracellular melatonin and its intermediates increased at the primordial (P) and fruiting body (FB) stages. However, the levels of most intermediates exhibited a notable decrease under CR stress. Several genes related to melatonin synthesis, excluding AADC (aromatic-L-amino-acid decarboxylase), were markedly upregulated at an early stage of CR stress but downregulated later. Compared to the mycelial stage, those genes were significantly upregulated at the P and FB stages. Additionally, exogenous melatonin promoted resistance to several abiotic stressors and P formation in T. guangdongense. This study is the first to report melatonin biosynthesis pathway in macro-fungi. Our results should help in studying the diversity of melatonin function and melatonin-synthesis pathways and provide a new viewpoint for melatonin applications in the edible-medicinal fungus.