Pakistan 169 for 6 (Babar 44, Shamsi 4-25) beat South Africa 164 for 8 (Miller 85*, Mahmood 3-40) by four wickets
Lahore: Tail-ender Hasan Ali hit two sixes in the penultimate over to help Pakistan clinch the Twenty20 international series against South Africa with a four-wicket win in the third and final match in Lahore on Sunday.
Hasan finished with a seven-ball 20 not out as Pakistan overhauled a 165-run target in 18.4 overs to win the series 2-1, South Africa's first T20I series defeat in Asia.
South Africa had previously won five and drawn one T20I series in Asia.
South Africa's late blip meant David Miller's robust 85 not out and spinner Tabraiz Shamsi's four wickets, which seemed to have put the tourists on the road to victory, were in vain.
Miller smashed seven sixes and five fours off just 45 balls to lift South Africa from a tottering 48-6. He helped add 116 for the last four wickets, taking South Africa to 164-8.
Shamsi then picked up 4-25 in four incisive overs and Pakistan needed 53 off the last six overs and 28 off the last three.
With 16 needed off the final two overs, Andile Phehlukwayo bowled a waist-high no-ball which Hasan hit for six to ease the pressure. He then hit a four and a six to seal victory.
Mohammad Nawaz finished with 18 not out, while skipper Babar Azam scored 44 and in-form Mohammad Rizwan made 42.
The victory is Pakistan's 100th in 163 matches, the first team to complete a century of wins in T20 internationals.
Azam's counterpart Heinrich Klaasen blamed his side's poor batting early on.
Pakistan, who opened with Haider Ali instead of Azam, raced to 51 off 6.1 overs before Shamsi dismissed Haider (15), removed Rizwan leg-before and bowled Hussain Talat for five to halt the home team's progress.
Rizwan, who hit 104 not out and 51 in the first two matches, struck seven boundaries, including two towering sixes, off 30 balls.
Pakistan won the first match by three runs while South Africa took the second by six wickets, both also played in Lahore.
South Africa lost their first six wickets for 48 and their seventh at 64.
Nawaz dismissed Reeza Hendricks (two) and Jon-Jon Smuts (one). Then debutant Zahid Mahmood removed Klaasen (nought) and Janneman Malan (27) off succesive deliveries in his first over.
When Phehlukwayo went for a duck, South Africa had lost four wickets for seven runs in just nine balls.
Miller added 17 for the eighth wicket with Bjorn Fortuin (10) before adding a further 58 for the next in just five overs with Lutho Sipamla who scored eight not out.
Miller hit four sixes in the last over bowled by Faheem Ashraf, which yielded 25.
Leg-spinner Mahmood took 3-40, left-arm spinner Nawaz claimed 2-13 and paceman Hasan finished with 2-29.