The fly-half kicked the winning penalty having touched down four minutes earlier.
The England fly-half had pounced four minutes from time by scoring a try that he converted, and then confirmed his team’s place in the competition’s round of 16 through a nerveless last-gasp strike.
The Gallagher Premiership champions looked as though they would pay a hefty price for captain Alex Dombrandt’s second-half yellow card.
Cardiff turned the game – played behind closed doors at the Arms Park – on its head while England international Dombrandt was off, scoring three quickfire tries.
But Quins dug deep as Smith added to earlier tries from Louis Lynagh, Tyrone Green, Luke Northmore and Danny Care while Smith kicked four conversions and a penalty for a 16-point haul.
Wing Owen Lane led the way for Cardiff, touching down twice as Wales head coach Wayne Pivac looked on, with forwards James Ratti, Dillon Lewis and Corey Domachowski also scoring and fly-half Jarrod Evans adding four conversions.
Cardiff, hit by player unavailability for their opening two European games before Christmas, were under immediate pressure as Smith twice made sharp half-breaks, and it was no surprise when he created an opening try after just six minutes.
Cardiff were stretched defensively, and Smith’s pinpoint kick was caught by Lynagh, who finished impressively before Smith converted to open up a seven-point lead.
But Quins were then rocked by an equalising Cardiff score just five minutes later as Lane powered his way over for a try that owed everything to his strength and elusiveness.
Evans’ conversion put Cardiff on level terms, and they struck again at the end of a lively opening quarter after centre Rey Lee-Lo surged clear in midfield, Adams took the move on, then Ratti touched down from close range.
A second successful Evans conversion meant Quins’ promising opening had evaporated, and Cardiff continued to play impressive front-foot rugby, led by Lee-Lo.
Quins, though, exerted sustained pressure through their forwards approaching half-time, and they pounced after Cardiff prop Dimitri Arhip was sin-binned following repeated team infringements.
Cardiff had defended strongly, but they were undone when Green stepped inside a last-ditch tackle for his team’s second try, and although Smith drifted the conversion attempt wide, Quins were firmly back in contention at 14-12 adrift.
Smith and company thought they had struck again within a minute of the restart, after a fine kick into space by the fly-half was gathered at pace by centre Joe Marchant, before Care dived over between the posts.
But the score was ruled out for Care being offside, and while it was a huge let-off for Cardiff, they were soon undone through more fine work by Smith, who set up an attack that ended with Dombrandt delivering a scoring pass to Northmore.
Cardiff had been rocked back on their heels, yet they delivered a strong response after Dombrandt was yellow-carded for a technical offence on Quins’ line, with Lewis scoring a third try and Evans’ conversion nudging his team back in front.
Quins were suddenly all over the place, and two more tries followed in rapid succession as Lane finished superbly at the corner flag, then Domachowski scored after a thrilling long-distance team move.
Evans converted the fourth Cardiff try, leaving Quins 12 points behind entering the final quarter.
They had to score next to realistically revive victory hopes, and it was Care who delivered, sniping his way through Cardiff’s defence, and another Smith conversion halved the deficit.
Hooker Kirby Myhill became the second Cardiff player to collect a yellow card as the clock ticked down, and Quins were camped inside the opposition 22 before Smith showed his class through a try, conversion and penalty to complete a stunning late fightback.