Television replays showed South African fielder Faf du Plessis rubbing the ball on his trousers which had a zip on them, in an apparent attempt to tamper with the ball.
Umpires Rod Tucker of Australia and England's Ian Gould called South African captain Graeme Smith, changed the ball and added five runs to Pakistan's score.
Pakistan were struggling at 62 for three with Dale Steyn about to start the 31st over when the umpires took notice of the ball and took the decision.
Under International Cricket Council (ICC) rule 42.1 on tampering, if an umpire spots a change in the condition of the ball but doesn't know who is the culprit, he will issue a first and final warning to the captain and the ball will be changed.
In case the ball is tampered with again, the bowling team will face a five-run penalty and umpires will not only change the ball again but the captain will be held responsible and reported.
But in this case the umpires saw Du Plessis rubbing the ball in an apparent move to tamper with the ball and they called Smith and docked five penalty runs.
An ICC spokesman confirmed the incident. "As per 42.1 of the ICC playing conditions, the umpires replaced the ball and fined South Africa five penalty runs for ball tampering," said an ICC spokesman.
Pakistan is the only other team to have been penalised with five penalty runs, during the infamous Oval Test against England in 2006.
In a sharp reaction to Australian umpire Darrell Hair's decision, Pakistan captain Inzamam-ul Haq walked off the field.
Hair then ruled that Pakistan had forfeited the match - the only such instance in Test cricket history.