The effect of perioperative acupuncture on accelerating gastrointestinal function recovery has been reported in colorectal surgery and distal gastrectomy (Billroth-II). However, the evidence in pancreatectomy and other gastrectomy is still limited. A prospective, randomized controlled trial was conducted between May 2018 and August 2019. Consecutive patients undergoing pancreatectomy or gastrectomy in our hospital were randomly assigned to the electroacupuncture (EA) group and the control group. The patients in the EA group received transcutaneous EA on Bai-hui (GV20), Nei-guan (PC6), Tian-shu (ST25), and Zu-san-li (ST36) once a day in the afternoon, and the control group received sham EA. Primary outcomes were the time to first flatus and time to first defecation. In total, 461 patients were randomly assigned to the groups, and 385 were analyzed finally (EA group, n = 201; control group, n = 184). Time to first flatus (3.0 ± 0.7 vs 4.2 ± 1.0,
P
<
0.001
) and first defecation (4.2 ± 0.9 vs 5.4 ± 1.2,
P
<
0.001
) in the EA group were significantly shorter than those in the control group. Of patients undergoing pancreatectomy, those undergoing pancreaticoduodenectomy and intraoperative radiation therapy (IORT) surgery benefitted from EA in time to first flatus (
P
<
0.001
) and first defecation (
P
<
0.001
), while those undergoing distal pancreatectomy did not (
P
flatus
=
0.157
,
P
defecation
=
0.007
) completely. Of patients undergoing gastrectomy, those undergoing total gastrectomy and distal gastrectomy (Billroth-II) benefitted from EA (
P
<
0.001
), as did those undergoing proximal gastrectomy (
P
=
0.015
). Patients undergoing distal gastrectomy (Billroth-I) benefitted from EA in time to first defecation (
P
=
0.012
) but not flatus (
P
=
0.051
). The time of parenteral nutrition, hospital stay, and time to first independent walk in the EA group were shorter than those in the control group. No severe EA complications were reported. EA was safe and effective in accelerating postoperative gastrointestinal function recovery. Patients undergoing pancreaticoduodenectomy, IORT surgery, total gastrectomy, proximal gastrectomy, or distal gastrectomy (Billroth-II) could benefit from EA. This trial is registered with NCT03291574.