Amla was on 63 when Temba Bavuma swept Yasir Shah for four to take South Africa past their target of 149 shortly before tea on the third day.
"They have brought some very good bowlers," said Elgar. "It could have gone either way but luck was on our side."
Pakistan made 181 in their first innings while South Africa countered with 223.
Sarfraz Ahmed, the Pakistan skipper, said his side had missed the chance to take control of the game after tea on the second day.
The tourists were on 100 for one in their second innings, a lead of 58.
But they lost nine wickets to be bowled out for 190.
"We had an opportunity to capitalise but we lost too many wickets in one session," said Sarfraz. "That cost us the match."
South Africa fast bowler, Duanne Olivier, was named man-of-the-match for his return of 11 wickets from both innings for the cost of 96 runs.
His haul overshadowed the record-breaking exploits of bowler Dale Steyn.
During the first innings, the 35-year-old took his 422nd wicket to eclipse Shaun Pollock as South Africa's leading Test wicket-taker.
South Africa skipper Faf du Plessis said: "Great Test and good to go one up. Duanne reminded me of what Mitchell Johnson did to us a few years ago when we played Australia. He makes it very uncomfortable for the batsmen."
The second Test begins in Cape Town on 3 January.