Babar century helps Pakistan beat South Africa off last ball