The off-spinner got rid of AB de Villiers for 37 and Quinton de Kock for a duck after South Africa resumed at their overnight 98-3 at the SSC ground in Colombo.
At lunch, the visitors were 163-5, trailing Sri Lanka by 258 runs in the first innings. Sri Lanka made 421 in their first knock.
Jean-Paul Duminy (three) was the other batsman not-out at the crease.
The Proteas, who won the first match in Galle by 153 runs to go 1-0 up in the two-Test series, added 62 runs in the first session while losing two wickets.
More From This Section
Amla broke the shackles with two consecutive fours off unorthodox spinner Ajantha Mendis and de Villiers followed suit, hitting paceman Suranga Lakmal for three boundaries in the same over.
De Villiers had a scare on 30 when he gloved Lakmal to the wicketkeeper but TV replays showed the ball had bounced just inches short of Niroshan Dickwella.
Amla completed his 28th Test fifty and first as captain with a single off Mendis, reaping the reward for batting with patience against the three-pronged spin attack.
But just when the partnership was building up, Perera's double-strike reduced South Africa to 150-5.
Perera struck again off the last ball of his over, knocking the off-stump of Quinton de Kock before he could even open his account.
Amla stood like a rock at one end, having faced 216 balls so far during his more than four-hour vigil at the crease.