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 Sangakkara caught by wicket-keeper Kamran Akmal in the space of 16 deliveries on a lively wicket freshened up by early morning rain.
Paranavitana, top scorer in Sri Lanka's first innings with 72, once again held the innings together with 49, but found little support from the top order.
Jayawardene failed to score and Sangakkara, leading Sri Lanka for the first time in a Test series, made nine and 14 in the two innings.
Sri Lanka were reduced to 101-5 when Thilan Samaraweera (34) and Tillakaratne Dilshan (22) put on 37 for the sixth wicket.
Sri Lanka's last three wickets added 61 valuable runs after the seventh wicket had fallen at 156 with the hosts just 106 runs ahead.
Angelo Mathews hit 27, Nuwan Kulasekera chipped in with 25 and Thilan Thushara remained unbeaten on 15.
Ajmal removed the dangerous Samaraweera to a catch in the slips before taking the last two wickets of Kulasekera and Mendis.
When the day's play started 30 minutes late due to a wet outfield, Pakistan broke through off the second ball when Malinda Warnapura edged Umar Gul to a diving Younus at second slip before a run had been scored.
Left-handed Paranavitana and Rangana Herath defied the Pakistani seamers to put on 68 for the second wicket before nightwatchman Herath was trapped leg-before-wicket by Younus just before lunch.
The three-match series is the first between the two nations since gunmen attacked the Sri Lankan team bus in the Pakistani city of Lahore on March 3 while the squad was en route to resume a Test match.
The attack left seven Sri Lankan players injured and eight Pakistani security forces dead.
The two teams recently contested the World Twenty20 final in England, in which Pakistan beat Sri Lanka by eight wickets.