Pakistan openers Taufeeq Umar (20) and Mohammad Hafeez (18) scored 42 runs in the remaining nine overs to trail Sri Lanka by 197 at the close.
Gul extracted enough movement off the Dubai International Cricket Stadium pitch to trouble the batsmen throughout his marathon 10-over opening spell, picking up three wickets before lunch.
Left-armer Junaid kept up the pressure and took the remaining two wickets to fall before the interval after Sri Lanka captain Tillakaratne Dilshan had won the toss.
Kumar Sangakkara, double-centurion in the drawn first test, was dropped on 27 off Saeed Ajmal by Taufeeq Umar and added 54 for the sixth wicket with debutant wicketkeeper-batsman Kaushal Silva (20) to stop the rot, after being reduced to 73-5.
Sangakkara, who looked solid and hit 11 boundaries during his knock of 78, holed out in the deep attempting a big hit against Ajmal.
Off-spinner Ajmal (3-45) and fellow left-arm spinner Abdur Rehman (2-40) combined to take the last five wickets in the remaining two sessions to put Pakistan firmly in control.
Number 10 batsman Chanaka Welegedara (48), whose previous highest score in tests was 11, added 75 for the ninth wicket with Rangana Herath (29) to take Sri Lanka past the 200-run mark.
Sri Lanka got off to a bad start when opener Lahiru Thirimanne fell lbw to Gul in the third over, before the lanky paceman struck twice in the ninth over to remove Tharanga Paranavitana and Mahela Jayawardene.
Junaid removed captain Dilshan and Angelo Mathews to compound Sri Lanka's misery.
Pakistan made one change to their side, dropping paceman Aizaz Cheema in favour of Rehman.
Sri Lanka suffered a major blow before the match when wicketkeeper-batsman Prasanna Jayawardene was ruled out for the remainder of the series due to a stomach injury.
Paceman Nuwan Pradeep was also dropped from the Sri Lanka team and was replaced by Dhammika Prasad.
Pakistan are playing "home" tests in the Gulf region because of security fears in their own country. The third and final match will be played in Sharjah from November 3.