Callum Sheedy landed a match-winning penalty with the final kick of the game as Cardiff completed a remarkable comeback in an epic Challenge Cup encounter at the Arms Park.
With just 15 minutes to go, they were trailing 26-12 and seemingly on course for a first home defeat of the season.
But tries from prop Danny Southworth and Player of the Match Dan Thomas tied things up to set up a grandstand finish.
Then, in the final minute, a huge scrum shove earned the hosts a penalty and fly-half Sheedy kept his nerve to slot the angled kick from some 35 metres out and secure a bonus point victory.
This was one real rollercoaster ride of a match.
Cardiff had raced into a 12-0 lead with early tries from lock George Nott and wing Tom Bowen.
But then Ulster rattled up 26 points either side of the break to pull well clear, only for the hosts to come back from the dead.
It was a night to remember for Wales legend Leigh Halfpenny who made his first appearance for Cardiff in 4,347 days.
His last outing for the club was way back in January 2014 against Exeter in the Heineken Cup. Since then, his glittering career has taken him to Toulon, the Scarlets, the Crusaders and Harlequins.
Now he’s back at the Arms Park, having come on board as a player-coach – a deal that was extended this week.
So it was that he launched his second playing spell in Blue & Black by lining up at No 15 against Ulster, only nine days away from his 37th birthday.
After a very tidy return, which saw him play a part in Bowen’s try, he left the field just before the hour mark to a warm ovation.
It was also a landmark occasion for Ben Thomas, Keiron Assiratti and Rory Thornton who all made their 100th appearances for the club.
Cardiff were much changed from last weekend’s Challenge Cup defeat away to Stade Francais with skipper Alun Lawrence, fellow back row James Botham, lock Thornton and prop Rhys Barratt the only survivors.
There was one late amendment among the replacements, with Wales flanker Alex Mann being ruled out and Lucas De La Rua taking his spot on the bench.
In-form Ulster, who arrived in Wales having won five of their six matches this season, also rang the changes from their 61-7 demolition of Racing 92, eight of them in total.
When the game got underway in front of a crowd of 6,425, it was the hosts who opened the scoring on seven minutes thanks to a second row double act.
After No 8 Lawrence smashed his way up the middle, the ball was moved right for Thornton to put his fellow lock Nott into a hole with a short pass.
There was nothing but open space in front of the big man and he galloped over from 30 metres with Halfpenny adding the conversion.
Cardiff then extended their lead on 13 minutes through a score that featured an unusual build-up and a class finish.
It was a case of rugby pinball as Ben Thomas’ pass deflected off the head of opposite number Ben Carson and then forward off the chest of Halfpenny who picked up the pieces and fed teenager Tom Bowen, 17 years his junior.
Then it was down to the predatory prowess of 19-year-old winger Bowen who grubbered the ball down the touchline and outpaced Werner Kok to gather it as it bounced up and touch down.
There was a lengthy wait as the officials conducted a video review of the ricochets, but in the end it was decided the score should stand, giving Bowen his sixth try of the season.
While it was very much all Cardiff in the first quarter, it was to be a different story in the second.
Ulster began their revival with a try from prolific hooker Tom Bowen, who holds the record for the most tries in a URC season – 16 in 2022-23.
He still had plenty of work to do as he broke away from a lineout drive, but he did it well, forcing his way over with Jake Flannery converting.
Ulster were now up and running and twice came close to another score, with flanker David McCann held up over the line and skipper Stewart denied by a timely intervention from Botham.
Then, at the other end, Cardiff scrum-half Aled Davies was unable to grasp what would have been a scoring pass from Harri Millard after a searing Bowen break.
But it was to be the visitors who had the final word in the half as centre James Hume pounced on a spillage from Sheedy and sprinted home from just inside his own half, with Flannery converting to give them a 14-12 interval lead.
Ulster extended their advantage in the third quarter as South African winger Kok was twice worked over in the right corner.
With 15 minutes to go, Cardiff hit back as replacement prop Danny Southworth smuggled his way over following a series of forward carries off a tap penalty, with Sheedy converting.
Both teams had players sin binned as Botham and Ulster lock Harry Sheridan were yellow carded following a mass altercation on the touchline in front of the main stand.
The visitors were reduced to 13 when centre Carson was yellow carded for a deliberate knock on and Cardiff immediately capitalized with flanker Dan Thomas rounding off a lineout drive.
It was then over to Sheedy who landed the conversion and that dramatic last-gasp penalty.

