The pace-spin duo grabbed three wickets each as Sri Lanka were bowled out for 217 in their second innings on the third day, leaving Pakistan a modest victory target of 168 runs.
The tourists, who have never lost a Test series in Sri Lanka, ended the day at 71-2 and have two days to score the remaining 97 runs with eight wickets in hand for a 1-0 lead in the three-match series.
Sri Lanka removed Khurram Manzoor and skipper Younus Khan in the space of three runs to make Pakistan 39-2, but opener Salman Butt (28 not out) and veteran Mohammad Yousuf (12 not out) saw out the day.
Teenage left-arm fast bowler Aamir broke the back of Sri Lanka's batting with three wickets for two runs, before off-spinner Ajmal polished off the tail in front of some 5,000 stunned home fans at the Galle International Stadium.
The 17-year-old Aamer followed his three wickets in the first innings with a hostile burst soon after lunch.
He had Tharanga Paranavitana, Mahela Jayawardene and skipper Kumar S