Dublin: Pakistan denied Ireland an unlikely win in their inaugural Test after a nervous start to their run chase at Malahide on Tuesday.
A stunning opening bowling spell raised the tantalising prospect of Ireland becoming the second side to win on Test debut as Pakistan were reeling on 14-3.
But Imam-ul-Haq and Babar Azam shared a 126-run fourth wicket stand as Pakistan battled to their winning target of 160.
The match ended just as rain clouds began to darken over Malahide and the prospect of a draw had surfaced.
Pakistan struggled to complete a relatively modest run-chase against Sri Lanka last year and a furious bowling spell from Ireland's seamers sent a thrill of expectation around the ground.
Tim Murtagh dismissed Azhar Ali with the fourth ball of the innings, that was nicked to first slip, before extra height from Boyd Rankin accounted for Haris Sohail and Murtagh returned to bowl Asad Shafiq in the next over.
Babar joined Imam in the middle and the pair took the sting out of the Irish attack before both players built half-centuries.
Babar was run out on 59 but Imam looked composed throughout as he hit the winning runs to clinch victory by five wickets.
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.
The Irish, resuming on 319 for seven, saw Abbas then take three wickets for 12 runs in 22 balls, the paceman finishing with an innings haul of five for 66 in 28.3 overs.
Only three sides in the 141-year history of Test cricket had won after being made to follow on and only one men's team -- Australia in the inaugural Test against England at Melbourne in 1877 -- had enjoyed a victory in their debut match at this level.