Ireland 130 and 319 for 7 (K O'Brien 118*, Thompson 53, Amir 3-57) lead Pakistan 310 for 9 dec by 139 runs
Scorecard and ball-by-ball details

Ireland's rise from World Cup giant-killers to fully fledged Test nation has featured a band of familiar faces, few more recognisable than that of Kevin O'Brien. From Sabina Park 2007, when he was in the middle to help seal victory over Pakistan, to Bangalore 2011, when his 50-ball hundred (and pink dye job) slayed England, O'Brien has been at the centre of Ireland's story. Now he is the first Irishman to score a Test hundred.

This time the hair was a more natural, russet colour, as O'Brien removed his helmet to soak in the applause at Malahide. Having been made to follow on 180 runs behind, many would have written off Ireland's chances of taking the game into day five; instead, they will resume their second innings with a lead of 139 and the prospect of giving Pakistan an awkward run chase - at the very least.

A thick outside edge through point off Mohammad Amir brought O'Brien to a milestone that held significance far beyond the personal. 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.

It took a ripping delivery from legspinner Shadab Khan to remove Thompson, bowled playing back as the ball lurched from the rough outside off stump, but No. 9 Tyrone Kane then dug in stoically for an hour and a half for 8 off 67 balls to frustrate Pakistan further.

Having taken four wickets in brisk fashion during the morning, and then a couple more after lunch, Pakistan's seamers went flat as the day wore on. Thompson was dropped on 6 off the bowling of Rahat Ali, a low edge to Sarfraz Ahmed's left, but was largely untroubled in recording a maiden Test half-century that was marked by growing confidence. Rahat was particularly innocuous and Thompson's thump for four to bring up fifty and the century stand with O'Brien summed up the shift in fortunes.

As in the first innings, when he top-scored with 40, O'Brien was an assured presence at the crease, his bat seemingly as wide as St James's Gate, temperament as reliable as the stout Dublin is most famous for. Rather than booming drives and pulls, he played softly and watchfully, gliding several boundaries through third man or off his pads. A tap through mid-on secured him fifty, another first for Ireland in Tests.

The spirit epitomised by Gary Wilson's first-innings resistance, batting at No. 9 with an injury, and the opening stand second time in between Ed Joyce and William Porterfield coursed through the Irish effort. While Amir bowled through the pain of a knee problem, Shadab found turn and Mohammad Abbas continued his penetrative start to life as a Test cricketer, Pakistan became increasingly anguished.

A thick edge through vacant second slip in the penultimate over of the day brought a scream of annoyance from Amir, as a weary O'Brien stared blankly down the pitch. O'Brien came close to playing on and being lbw to Abbas in the next over but there was no succour for Pakistan.

Such an outcome seemed unlikely after an eventful morning session, in which Ireland extended their opening stand to 69 before Joyce was run out by a direct hit from Faheem Ashraf at mid-on. In a match that has seen plenty of dodgy running, Joyce was perhaps unlucky to be the only man run out (so far); he ended up fractionally short after calling a tight single, with Ashraf's throw aiming at one stump doing just enough to dislodge the bails.

That gave Pakistan an opening, which they quickly exploited. Andy Balbirnie made an unwanted sort of history when he was hit on the pads and given lbw against Abbas for the second time in the match, becoming the first Ireland batsman to register a Test pair. Keen to avoid a similar fate, Niall O'Brien's first run came via a risky single that left him on his knees sporting a wry grin and muddy sweater.

The elder O'Brien made it into double-figures before being detonated from the crease by Amir, who flattened the stumps with a delivery that reversed in to the left-hander. William Porterfield, who laboured for 120 balls over his 32, was given a torrid time during Amir's initial seven-over spell and eventually succumbed to a wicked outswinger that Sarfraz held on to.

Ireland at that stage were 95 for 4, still 85 from putting Pakistan in for a second time. Paul Stirling was lbw shortly after lunch, Abbas locating his pad fractionally before bat came into play, and when Wilson was caught slip off Amir it was 157 for 6 and the result all but certain. Then Kevin O'Brien's scriptwriter intervened again.