Chasing a 134-run victory target, Pakistan were reeling at 76 for seven in the 15th over when Gul joined Umar Akmal (43 not out) and they added 49 runs off 4.3 overs to turn the group two match on its head.
Number nine batsman Gul blasted three sixes and two boundaries in his entertaining cameo before being dismissed in the 19th over. Akmal batted till the end as Pakistan scampered home with two balls to spare.
"I knew he can hit the ball hard. He showed character today," Pakistan captain Mohammad Hafeez said of man-of-the-match Gul.
"Even if it's not with his bowling, he can still contribute for the team. It's a very good sign for us."
Pakistan needed nine runs off the last over sent down by Morne Morkel and Akmal hit a six off the second delivery, a full toss, before taking a single.
Saeed Ajmal edged the fourth delivery to third man boundary to score the winning run.
"The bowlers did a great job and we restricted South Africa to a total we can chase. South Africa played well and anybody could have won it but in the end, Pakistan was lucky to get this," Hafeez said.
It capped a stunning comeback by Pakistan who lost their top three batsmen in a span of seven balls.
Dale Steyn (3-22) drew first blood removing Imran Nazir (14) before left-arm spinner Robin Peterson dismissed Hafeez (15) and the scoreless Nasir Jamshed in his first over to peg them back.
South African bowlers maintained the pressure and were rewarded with wickets until Gul and Akmal mounted a spectacular counter-attack.
"Unfortunately we lost the momentum in the last five overs," South Africa skipper AB de Villiers rued.
"I'm proud of the way we fought back into the game, especially after the bad start. We will be back fighting in the next two games for sure," he said.
Earlier opting to bat first, South Africa overcame a top order crisis to post 133 for six wickets, a total built largely around J.P. Duminy's 38-ball 48 that included two sixes and as many boundaries.
The Proteas struggled against Pakistan's four-pronged spin attack and were 66 for four wickets in the 13th over before Duminy and captain AB de Villiers (25) scored some brisk runs to help them to a below-par total on a slow track at the Premadasa Stadium.