Pakistan 272 (Faheem 78*, Nortje 5-56) and 298 (Rizwan 115*, Linde 5-54) beat South Africa 201 (Bavuma 44*, Hasan 5-54) and 274 (Markram 108, Bavuma 61, Hasan 5-60, Afridi 4-51) by 95 runs

Hasan Ali's ten-wicket match haul topped stubborn resistance from two of South Africa's Test captaincy candidates, Aiden Markram and Temba Bavuma, to secure Pakistan's first series win over South Africa since 2003. Hasan triggered a collapse with the second new ball that saw South Africa lose seven wickets for 33 runs and fall 96 runs short of what would have been the highest successful chase in Pakistan. They had lost the first Test, in Karachi, by seven wickets.

Despite the defeat, South Africa would be especially pleased with Markram's performance. He scored his fifth Test century - his first since March 2018 as well as the first in the subcontinent - to keep hopes of an unlikely upset win alive. Markram and Bavuma, who scored 61, shared a fourth-wicket stand of 106 runs and looked comfortable at the crease against the old ball. But neither could negotiate the fresh ball on a surface that was keeping low, and their dismissals opened up the lower order and allowed Pakistan to seal the series 41 minutes before tea on the final day.

South Africa have now lost four of their last five series and ten of their last 13 Tests and are going to have an extended break from the longest format with no games scheduled for the rest of the summer following Australia's decision to postpone indefinitely a proposed series in March. South Africa had hoped to use this season's Tests to identify a suitable long-term Test captain after Faf du Plessis stepped down last year. Quinton de Kock, who took over in a temporary capacity, is expected to be relieved from the role after managing just 74 runs in four Tests as captain.

Things could hardly have been worse for de Kock, who was dismissed for a first-ball duck chasing a full, wide Hasan delivery that he should have left. De Kock's dismissal came the ball after Markram was caught at second slip, prodding at a Hasan delivery that kept low. That was in Hasan's first over with the second new ball, and the second over with it overall, and Pakistan needed just ten more overs to finish South Africa off.

The speed of, and shot selection that led to, South Africa's collapse remains a concern - they had lost five for 37 in the first innings and seven for 87 and nine for 70 in Karachi. While the Rawalpindi pitch got better for batting as the match progressed, it kept low throughout and only Markram and Bavuma were able to adapt to the bounce on the fifth day.

Rassie van der Dussen applied himself well on the penultimate day of the match and was on 48 overnight but fell to the third ball of the day. Hasan beat his inside edge with late swing to end a 94-run second-wicket stand with Markram and give Pakistan the breakthrough early on the final morning. Two overs later, Hasan had du Plessis trapped lbw with an incoming delivery that stayed low. Du Plessis reviewed but ball-tracking showed middle and leg stump would have been disturbed.

Instead of sending in de Kock, South Africa opted to stick to the same batting order they had used in the first innings with Bavuma coming in at No. 5. Bavuma batted with more intent than he has come to be known for, and was especially strong on the back foot and the sweep. He played a strong supporting role for Markram, whose confidence grew as his innings developed.

Markam was comfortable enough to play the ball late, kept out yorkers from Shaheen Shah Afridi - who took 4 for 51 for the innings - and took on Yasir Shah, cutting him for four and dancing down the track to loft him over long-off for six. His hundred came up the ball before lunch with a single to midwicket to break a century drought that has stretched 26 innings. But his celebrations were subdued, with the knowledge there was still work to do.

He only added eight runs to his total after lunch before Hasan had the second new ball in hand and lured Markram into playing a stroke. De Kock's dismissal off the next ball meant that Bavuma and the lower-order batsmen were left with 129 runs to get. That became far too much when Bavuma played at Afridi outside off and was caught behind.

Afridi reaped rewards for his disciplined bowling in the off-stump channel and got the wickets of Keshav Maharaj and Kagiso Rabada later on, to go with the scalp of Dean Elgar on the fourth evening. But the day belonged to Hasan, whose five-for came when he had George Linde caught at point and he celebrated his usual bomb-explosion celebration to a baby-cradle one in a nod to his impending fatherhood. The entire squad did the same once victory had been completed, with Babar Azam joining Fazal Mahmood (vs West Indies, 1959), Mushtaq Mohammad (vs New Zealand, 1976), Javed Miandad (vs Australia, 1980) and Saleem Malik (vs New Zealand, 1994) in the list of Pakistan captains to win their maiden Test series at the helm.

Shah, who had gone wicketless as the pacers did the damage, finished the game when he beat Wiaan Mulder's slog to hit timber in the 92nd over. The win, Pakistan's first against South Africa in over 17 years, means they have recorded series wins over every Test side in the last 15 years. As for South Africa, it was their 21st successive failure to boss a 200-plus (they had been set 370) chase; the last time they managed one was back in 2011 against Australia. They have lost 18 of those matches.

Firdose Moonda is ESPNcricinfo's South Africa correspondent