But at lunch Pakistan were 52 for three, still needing a further 108 runs to win, with left-handed opener Imam -- the nephew of selection chief and former Pakistan batsman Inzamam-ul-Haq -- 33 not out and Babar Azam nine not out.
The umpires checked for a no-ball but Rankin had bowled a legal delivery, albeit only just.
And 13 for two became 14 for three in 4.3 overs when Middlesex paceman Murtagh produced a superb delivery to bowl Asad Shafiq between bat and pad for one.
But the 22-year-old Imam, who bats in spectacles, showed a maturity beyond many of his more experienced top-order colleagues.
He struck two fine fours off Stuart Thompson, a square cut followed by a square drive.
Murtagh took two for 27 in an unchanged nine-over spell.
Earlier, Ireland were dismissed for 339 in their second innings.
Kevin O'Brien, who on Monday became the first Ireland batsman to score a Test hundred, fell to his first ball Tuesday as Mohammad Abbas had him caught by Haris at slip for 118.
Ireland had been in danger of an innings defeat at 95 for four but a seventh-wicket partnership of 114 between O'Brien and Thompson (53) turned the tide. The Irish, resuming on 319 for seven, lost their last three wickets for 20 runs on Tuesday.
Abbas did all the damage in a burst of three wickets for 12 runs in 22 balls, the paceman finishing with innings figures of five for 66 in 28.3 overs.
Only three sides in the 141-year history of Test cricket have won after being made to follow on and only one men's team -- Australia in the inaugural Test against England at Melbourne in 1877 -- have enjoyed a victory in their debut match at this level.
Ireland were facing an innings defeat when O'Brien came in at 95 for four but, ably assisted by Thompson, who made 53, he turned the tide during a seventh-wicket partnership of 114.
But after Tyrone Kane had seen out the opening over, O'Brien fell to his first ball of the day when, going down on on to attempt a slashing drive off a wide half-volley from Abbas, he was held by Haris.
A crestfallen O'Brien bowed his head in frustration and dropped his bat as he trudged off the field.
But the 34-year-old had every reason to be proud of an innings that spanned five hours, 44 minutes and saw him face 217 balls with 12 fours. Abbas had Rankin, trying to leave, playing on before bowling Kane to end the innings.
(This story has not been edited by Business Standard staff and is auto-generated from a syndicated feed.)