Umar Akmal added a quick-fire 42, but it was Pakistan's cool under pressure at the end that sealed their triumph as a partnership of 110 between Amla and De Villiers looked to have put South Africa on track to come from behind to win and stay alive in the series.
Saeed Ajmal struck for the crucial wicket of Amla on the last ball of the penultimate over and Khan had JP Duminy caught off the second ball of the last over as Pakistan fought until the end to make sure of its moment in history.