Pakistan debutant Imam-ul-Haq saw his side to victory as Ireland's first match in Test cricket ended in a gallant last-day defeat by five wickets at Malahide on Tuesday.
Pakistan, set a seemingly modest 160 to win after earlier making Ireland follow-on, collapsed to 14 for three before lunch on the fifth day.
But 22-year-old left-handed opener Imam, the nephew of Pakistan selection chief and former Test batsman Inzamam-ul-Haq, responded to the pressure with 74 not out -- his maiden fifty at this level.
Together with Babar Azam, who made 59 after being dropped on nine, he kept Ireland at bay during a fourth-wicket stand of 126.
Imam hit the winning runs as grey clouds threatening rain hung over Malahide.
Victory was a good way for Pakistan to prepare for the first of a two-Test series against England at Lord's starting on May 24.
"Batting is very difficult here so a great knock from Imam and Babar," said Pakistan captain Sarfraz Ahmed at the presentation ceremony.
As for the England series the wicket-keeper added: "We need this kind of performance, so in'shallah we are ready for that tour."
Ireland captain William Porterfield was proud of his side's second innings resilience, particularly after they were dismissed for 130 in their first innings.
"We had a hell of a lot of good things coming out of the game," he said. "I couldn't have asked any more of the bowlers, we'd have liked to have the opportunity to bat that first innings again, but we showed character, and brought that into our first Test."
Just four balls into their chase, Pakistan saw Azhar Ali edge Tim Murtagh to Paul Stirling at first slip.
Haris Sohail (seven) fell next, caught in the gully by Ed Joyce off towering fast bowler Boyd Rankin as Ireland's two former England internationals combined.
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.
- Shock on the cards -
At that point Ireland looked capable of becoming only the fourth team in the 141-year history of Test cricket to win after following on.
They needed to grasp every chance but Babar was dropped on nine in the slips by Andrew Balbirnie.
Imam, having cover driven a four off Rankin, completed a 61-ball fifty with seven boundaries.
But with Pakistan 20 runs shy of victory, a mix-up saw Babar run out.
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 on Tuesday as Mohammad Abbas had him caught by Haris at slip for 118 after the batsman edged an attempted slashing drive off a wide half-volley from Abbas.
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.
His exit was the start of a three-wicket burst from Abbas, who wrapped up the innings on his way to figures of five for 66 in 28.3 overs
"I think a slightly better morning for us and we could have pushed Pakistan even harder," said O'Brien.
"I struggled the most this morning trying to forget about yesterday, it was mentally draining. I just played the situation where we needed to stick in and dig around. I'm renowned for slogging the ball into the stands but I reined myself in a little bit!"
Ireland's performance in giving Pakistan a scare was put into contest by the fact only one team had ever won their first Test match -- and that was when Australia beat England by 45 runs in the inaugural Test at Melbourne back in 1877.
Babar Azam made a half century to help steer Pakistan to victory in Ireland's first test.