Amir's three wickets earlier in the day had put Pakistan into a position from which an innings win seemed entirely possible - even likely - but O'Brien and Stuart Thompson combined for a 114-run rearguard that not only pushed Ireland in front and burnished local pride, but allowed them to sleep on the possibility of another famous upset.
The beaten Ireland captain William Porterfield is remaining positive: "It would have made for a very interesting game had lead got close to 200 runs and had we been able to break that partnership [between Imam-ul-Haq and Babar Azam] it could be a very interesting finish".
Together with Babar Azam (59) he kept Ireland at bay during a fourth-wicket partnership of 126.
Add in the fact that on only three occasions previously has a team that was forced to follow on - as Ireland were after being bowled out for 130 in the first innings - gone on to win a Test match, one in each of the last three centuries.
The visitors, who will take on England in a two-Test series starting on 24 May, were left reeling by Murtagh and Rankin's opening spell.
Although the crowd was small at that stage of the morning, Ireland's supporters were making plenty of noise as Tim Murtagh took the new ball from the Dublin Road end, tasked with getting among the Pakistan top order.
Babar was dropped on nine runs in the 19th over when Balbirnie could not hold a catchable chance and the right-hander immediately made Ireland pay by thumping two of his next four deliveries through the covers for boundaries.
USA ready to offer security assurances to DPRK: Pompeo
The top USA diplomat did not elaborate, but his comment could refer to the type of assurances North Korea has sought in the past. Pompeo said private-sector Americans could help rebuild North Korea's energy grid and develop the country's infrastructure.
Yet the controversial decision to shrink the 2019 World Cup in England to a 10-team tournament and Ireland's failure to come through an increasingly competitive qualifying tournament where only two spots were available means Porterfield's men will be on the outside looking in next year.
From there, they dragged themselves back into the match and played their way into contention going into the final day against a Test nation stalwart as Pakistan.
Kevin O Brien soon reached his ton and became the first Irish cricketer to score a test century, and that too in his debut match.
Boyd Rankin followed up closely with another outing leaving Pakistan 13-2 - before Murtagh claimed his second of the innings bowling out Asad Shafiq as Pakistan moved to 14-3. "Unfortunately, he came back after five years (out) and since he came back he has played every format for us", said Mahmood.
Ireland players after Pakistan secured a five-wicket win.
"We want him to play Test cricket because he's our number one bowler", said Mahmood. Test cricket needed a good story and Ireland provided this with a respective performance and subsequent justification of their status in Test cricket.
Only three teams have won a test after following on - England in 1894 and 1981, and India in 2001.