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.
Just four balls into their chase, Pakistan saw Azhar Ali edge Tim Murtagh to Paul Stirling at first slip.
Haris Sohail (seven) fell next, well taken 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.
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.
Ireland needed to take every chance that came their way but, with Babar yet to add to his interval score, he was dropped in the slips by Andrew Balbirnie.
Imam, having cover driven a four off Rankin, completed a 61-ball fifty with seven boundaries — the third time he had reached the landmark this tour after half-centuries in warm-up matches against Kent and Northamptonshire.
Babar, cashed in to make a 61-ball fifty.
But with Pakistan 20 runs shy of victory, a mix-up saw Babar run out.
There was still time for Ireland to take a couple more wickets but by then the match had escaped their grasp.
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.
— Slashing drive —
Ireland were facing an innings defeat when man-of-the-match O’Brien came in at 95 for four but, ably assisted by Stuart Thompson, who made 53, he turned the tide during a seventh-wicket partnership of 114.
But after Tyrone Kane had seen out Tuesday’s opening over from Mohammad Amir, who took his 100th Test wicket on Monday, Abbas struck.
O’Brien going down on one knee to attempt a slashing drive off a wide half-volley from Abbas, nicked to 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.