Root-knot nematodes (RKNs) are a vital pest that causes significant yield losses and economic damage to potato plants. The use of chemical pesticides to control these nematodes has led to environmental concerns and the development of resistance in the nematode populations. Endophytic fungi offer an eco-friendly alternative to control these pests and produce secondary metabolites that have nematicidal activity against RKNs. The objective of this study is to assess the efficacy of Aspergillus flavus (ON146363), an entophyte fungus isolated from Trigonella foenum-graecum seeds, against Meloidogyne incognita in filtered culture broth using GC–MS analysis. Among them, various nematicidal secondary metabolites were produced: Gadoleic acid, Oleic acid di-ethanolamide, Oleic acid, and Palmitic acid. In addition, biochemical compounds such as Gallic acid, Catechin, Protocatechuic acid, Esculatin, Vanillic acid, Pyrocatechol, Coumarine, Cinnamic acid, 4, 3-indol butyl acetic acid and Naphthyl acetic acid by HPLC. The fungus was identified through morphological and molecular analysis, including ITS 1–4 regions of ribosomal DNA. In vitro experiments showed that culture filtrate of A. flavus had a variable effect on reducing the number of egg hatchings and larval mortality, with higher concentrations showing greater efficacy than Abamectin. The fungus inhibited the development and multiplication of M. incognita in potato plants, reducing the number of galls and eggs by 90% and 89%, respectively. A. flavus increased the activity of defense-related enzymes Chitinas, Catalyse, and Peroxidase after 15, 45, and 60 days. Leaching of the concentrated culture significantly reduced the second juveniles’ stage to 97% /250 g soil and decreased the penetration of nematodes into the roots. A. flavus cultural filtrates via soil spraying improved seedling growth and reduced nematode propagation, resulting in systemic resistance to nematode infection. Therefore, A. flavus can be an effective biological control agent for root-knot nematodes in potato plants. This approach provides a sustainable solution for farmers and minimizes the environmental impact.