The tourists, who were bowled out for 90 in their first innings, hit back to dismiss Sri Lanka for 240 and then cruised to 178-1 in their second knock by stumps on the second day.
Left-handed Alam led the way with a determined 102 not out to give Pakistan a lead of 28 runs with nine wickets in hand on a dry pitch that appeared to have eased out under the hot sun.
The fearless 23-year-old from Karachi moved to 98 by lofting left-arm spinner Rangana Herath for a six over mid-wicket and then flicked the next ball for two runs to reach the coveted century.
Alam put on 85 for the first wicket with Khurram Manzoor who made 38 before he was caught behind by wicket-keeper Tillakaratne Dilshan off Herath.
Skipper Younis Khan, who was dismissed for zero in the first innings, was unbeaten on 35 at stumps after adding 93 for the unbroken second wicket with Alam.
Sri Lankan captain Kumar Sangakkara rotated his seam and spin attack in short bursts to gain quick breakthroughs, but Pakistan’s batsmen were not found wanting a second time.
Pakistan’s fightback was launched by seamer Umar Gul and off-spinner Saeed Ajmal, who took four wickets each to keep Sri Lanka’s first innings in check.
The hosts lost their last seven wickets for 63 runs after starting the day at a comfortable 164-3.
Gul extracted reverse swing with the old ball on the dry wicket to rip through the innings, including the wickets of Nuwan Kulasekera and Herath off successive deliveries.
Thilan Thushara denied Gul a hat-trick and Angelo Mathews hit a defiant 27 towards the end to boost Sri Lanka’s total from 220-8 before the innings ended 20 minutes after lunch.
Sangakkara plodded from his overnight score of 81 to 87 but was denied a 19th Test century when Gul pegged back his off-stump with a reverse-swinging delivery that came in sharply.
SCOREBOARD
PAKISTAN (I Innings): 90
SRI LANKA (I Innings, O.n: 164-3):
Warnapura lbw Gul 11
Paranavitana c Akmal b Ajmal 26
Sangakkara b Gul 87
Jayawardene c Manzoor b Ajmal 19
Samaraweera (run out) 21
Dilshan c Akmal b Ajmal 20
Mathews c Yousuf b Ajmal 27
Kulasekara c Misbah b Gul 11
Herath c & b Gul 0
Thushara lbw Rauf 1
Mendis (not out) 0
Extras (B-8, LB-1, NB-8) 17
Total (all out, 80 overs) 240
Fall of wickets: 1-28, 2-82, 3-133, 4-177, 5-188, 6-203, 7-220, 8-220, 9-227.
Bowling: Gul 18-1-43-4, Aamer 13-2-36-0, Rauf 11-1-38-1, Ajmal 31-5-87-4, Khan 7-1-27-0.
PAKISTAN (II Innings):
Manzoor c Dilshan b Herath 38
Alam (batting) 102
Younis (batting) 35
Extras (B-1, LB-1, NB-1) 3
Total (for 1 wkt, 53 overs) 178
Fall of wicket: 1-85.
Bowling: Kulasekara 9-2-25-0, Thushara 10-0-32-0, Mathews 6-0-20-0, Mendis 11-0-43-0, Herath 16-1-53-1, Paranavitana 1-0-3-0.