Abstract Background Hyperactivity of the hypothalamic–pituitary–adrenal (HPA) axis constitutes a pivotal response by surgical trauma, manifesting as a critical aspect of the acute stress reaction. This hyperactivity resulted in adverse surgical outcomes and is often associated with increased postoperative anxiety. Increased evidence suggests that Nesfatin-1 plays a crucial role in stress responses and stress-related psychiatric disorders. Electroacupuncture (EA) is widely used to alleviate stress responses and anxiety, although its mechanism of action remains unclear. This study aimed to assess the mechanisms by which hypothalamic Nesfatin-1 contribute to the alleviation of HPA axis hyperactivity and anxiety by EA. Methods Partial hepatectomy (HT) was performed to simulate surgical trauma, and EA was applied at Zusanli (ST36) and Sanyinjiao (SP6). The levels of hypothalamic Nesfatin-1, c-Fos, and corticotropin-releasing hormone (CRH) were detected, and serum adrenocorticotropic hormone (ACTH) and corticosterone (CORT) were regarded as indicators of HPA axis activity. Anxiety levels were assessed through open field tests (OFT), elevated plus maze (EPM), and light–dark box tests (LDBT). To investigate the role of Nesfatin-1, its expression was modulated using stereotactic viral injections or plasmid transfections. Transcriptome sequencing was employed to explore the downstream signaling pathways of Nesfatin-1. Additionally, brain cannula implantation was performed to facilitate targeted drug administration. Results Our findings demonstrated that EA reduced the hypothalamic overexpression of CRH and Nesfatin-1, as well as serum levels of ACTH and CORT. Additionally, it alleviated anxiety-like behaviors resulting from surgical trauma. We observed that overexpression of Nesfatin-1 in the hypothalamic paraventricular nucleus (PVN) triggered hyperactivity of the HPA axis and anxiety. Conversely, knocking down Nesfatin-1 in the PVN reversed these effects caused by surgical trauma. Transcriptome sequencing identified the extracellular regulated protein kinases (ERK)/cAMP-response element binding protein (CREB) pathway as a key mediator in the impacts of surgical trauma and EA on the hypothalamus. Both in vivo and in vitro studies showed that overexpression of Nesfatin-1 activated the ERK/CREB pathway. Furthermore, administering ERK or CREB inhibitors into the PVN mitigated HPA axis hyperactivity and anxiety-like behaviors induced by surgical trauma. Finally, EA was observed to decrease the phosphorylation levels of ERK and CREB in the PVN. Conclusion EA alleviates HPA axis hyperactivity and anxiety-like behaviors caused by surgical trauma through inhibition of Nesfatin-1/ERK/CREB pathway in the hypothalamus.