Hafeez hit 63 off 41 balls with three sixes and Akmal hammered 64 off 38 deliveries with four sixes as Pakistan made 176 for four, easily their best total in four 20-overs matches in the space of ten days against the same opponents -- two of them in the United Arab Emirates. South Africa won all three previous matches.
Afridi took three for 28 with his brisk leg-spinners after South Africa had raced to 58 for no wicket in the first seven overs of their reply.
The hosts finished with 170 for four -- losing by six runs - thanks largely to an unbroken stand of 53 off 28 balls between JP Duminy and David Miller. But they were never really in contention, despite scoring 19 off the penultimate over, bowled by Junaid Khan.
Seventeen were needed off the final over and Sohail Tanvir bowled a series of yorkers and low full tosses to restrict South Africa to 10 runs, which included a boundary off the last ball.
"We have been working hard," said Pakistan captain Hafeez, who was named man of the match and man of the series. "We knew we had to win this game to stay positive for the rest of the series."
Pakistan, who were beaten 4-1 in a one-day series and 2-0 in a Twenty20 series in the United Arab Emirates before travelling to South Africa, start a three-match one-day series at the same venue on Sunday.
Hafeez and Akmal put on 102 off just 57 balls for the third wicket as the South African bowlers were put to the sword.
The innings gained momentum when Wayne Parnell was no-balled in the seventh over and Hafeez slammed the free hit over the square leg fence for the first six.
Hafeez hit two successive straight sixes off left-arm spinner Aaron Phangiso in the 12th over before Akmal followed suit in an over which cost 21 runs.
Three overs later Morne Morkel conceded 22 runs as Akmal hit successive deliveries for six, four and six again.
Hashim Amla and Quinton de Kock gave South Africa a fast start but Afridi struck with his first delivery when De Kock swept him to deep backward square leg. Afridi took wickets in both his next two overs, having Faf du Plessis caught at midwicket and bowling AB de Villiers.
Amla went on to make 48 before he was caught in the deep off Saeed Ajmal, leaving Duminy (47 not out) and Miller (22 not out) with an improbable task.