The experienced slow bowler dismissed a well-set Mahela Jayawardene (47) with some extra spin and bounce and then baffled Dinesh Chandimal (11) with a doosra to reduce Sri Lanka to 166 for five at one stage.
Captain Angelo Mathews (24) and wicketkeeper Prasanna Jayawardene (28) held firm with an unbroken sixth-wicket stand of 54 runs to deny Pakistan further success.
Ajmal was confident he had Prasanna out leg before and the umpire agreed too, but the batsman reviewed the decision successfully, as replays showed the ball was bouncing over the stumps.
Faced with some accurate bowling from Pakistan, Sri Lanka found scoring difficult on a slow and low pitch at the Sharjah stadium after Mathews opted to bat after winning the toss.
Mohammad Talha, playing only his second Test, struck early to get rid of Kaushal Silva (17) while left-arm spinner Abdur Rehman dismissed Dimuth Karunaratne (34) immediately after lunch.
Kumar Sangakkara (52) added 60 for the third wicket with Mahela to steady Sri Lanka's ship but hit paceman Junaid Khan tamely to mid-wicket after completing his 44th Test fifty.
Sangakkara, on 15, was lucky to survive an appeal for a catch after replays showed that he had gloved a sweep shot off Rehman. Pakistan opted not to challenge the umpire's not out verdict.
Mahela looked in good control against the spinners before failing to negotiate a ball from Ajmal which turned and bounced sharply to take the edge and reach Azhar Ali at short leg.
Sri Lanka, who lead the three test series 1-0, would have been in more trouble if Talha had managed to hold on to a return catch off Mathews with the batsman yet to open his account.
Scorecard
Sri Lanka (1st innings):
D. Karunaratne c Younus b Rehman 34
K. Silva c Sarfraz b Talha 17
K. Sangakkara c Manzoor b Junaid 52
M. Jayawardene c Azhar b Ajmal 47
D. Chandimal c Shafiq b Ajmal 11
A. Mathews not out 24
P. Jayawardene not out 28.
Extras: (lb-5, nb-2) 7
Total: (five wickets; 90 overs) 220
Still to bat: D. Perera, R. Herath, S. Eranga, S. Lakmal
Fall of wickets: 1-31, 2-65, 3-125, 4-159, 5-166
Bowling: Junaid 17-3-44-1, Talha 20-2-64-1 (2nb), Ajmal 29-7-62-2, Rehman 24-9-45-1
Sri Lanka lead the series 1-0
Image: Saeed Ajmal
Photograph: Shaun Roy/Gallo Images/Getty Images
Younis, Misbah steady Pakistan but Sri Lanka in control
Jayawardene hundred puts Sri Lanka on top in Dubai
Shane Watson to miss three one-dayers against England
Dubai Test: Sri Lanka win to go 1-0 up against Pakistan
Hafeez returns to Pakistan squad for Sri Lanka Tests