A century by Babar Azam set up a three-wicket, last-ball win for Pakistan in the first one-day international against South Africa at SuperSport Park in Centurion on Friday.
The Pakistan captain hit a stroke-filled 103 off 104 balls and shared a second-wicket partnership of 177 with Imam-ul-Haq, who made 70.
But Babar's dismissal started a collapse in which fast bowler Anrich Nortje took four wickets in four overs.
From 186 for one, with more than 18 overs in hand to reach a target of 274, Pakistan slumped to 203 for five before Mohammad Rizwan and Shadab Khan steadied the innings again in a sixth-wicket stand of 53.
But both were dismissed before Faheem Ashraf hit the winning run off Andile Phehlukwayo off the last ball.
"There was a little bit of pressure but we will learn from that," Babar said through an interpreter at the after-match presentation.
Babar, who was named man of the match, praised his bowlers, who came within three runs of achieving his objective of restricting South Africa to a total of around 270 after he won the toss and sent them in.
"I told them to bowl hard lengths and there was some swing as well," he said.
"They took wickets in the power play and that helped us."
Rassie van der Dussen had to perform a rescue act for South Africa after the hosts struggled to 55 for four inside 15 overs.
He hit 123 not out, his maiden international century, to enable South Africa to score 273 for six.
Proteas captain Temba Bavuma admitted the loss of early wickets disrupted South Africa's aggressive intent.
"We didn't help ourselves with our batting effort in the first ten overs," he said.
The win gave Pakistan an important advantage in the three-match series, with South Africa set to lose five key players to the Indian Premier League after Sunday's second match in Johannesburg.