Taking the endophytic fungus UJ3-2, isolated from Urtica fissa, as the experimental material, this study aimed to explore the composition of its metabolites and the underlying mechanisms by which it inhibits Staphylococcus aureus. Initially, the MIC, MBC, inhibitory curves, biofilm growth, and extracellular nucleic acids and proteins of S. aureus in response to the metabolites were measured. Secondly, PI staining and SEM were used to evaluate the impact of the metabolites on the integrity of the cell wall and overall morphology of S. aureus. Additionally, UPLC-MS was employed to analyze the composition of the secondary metabolites. The UJ3-2 strain was identified as Xylaria grammica based on ITS sequencing and designated as Xylaria grammica UJ3-2. Our results revealed that the metabolites of UJ3-2 exhibited excellent in vitro antibacterial activity against S. aureus, with both MIC and MBC values of 3.125 mg/mL. The inhibitory curve confirmed that 1 MIC of UJ3-2 metabolites could completely inhibit the growth of S. aureus within 24 h. With increasing concentrations of UJ3-2 metabolites, the growth of S. aureus biofilms was significantly suppressed, and obvious leakage of nucleic acids and proteins was observed. PI fluorescence staining indicated that various concentrations of UJ3-2 metabolites disrupted the integrity of the S. aureus cell membrane. SEM observation revealed that the treated S. aureus surfaces became rough, and the bacteria shrank and adhered to each other, showing a dose-dependent effect. UPLC-MS analysis suggested that the main components of the fermented metabolites were 6-oxocineole (17.92%), (S)-2-acetolactate (9.91%), 3-methyl-cis,cis-muconate (4.36%), and 8-oxogeranial (3.17%). This study demonstrates that the endophytic fungus UJ3-2 exhibits remarkable in vitro antibacterial effects against S. aureus, primarily by enhancing the permeability of the S. aureus cell membrane, causing the leakage of its intracellular contents, and altering the bacterial surface morphology to inhibit the pathogen. The endophytic fungus UJ3-2 has a good antibacterial effect on S. aureus, which gives it certain application prospects in the screening and industrial production of new and efficient natural antibacterial active substances.