Jarrod Evans held his nerve to fire over a 45-metre penalty with the last kick of the game to give Cardiff their first win over Leinster since 2011.
Cardiff looked to have blown their chance when they conceded 14 points while No 8 James Botham was in the sin-bin but they finished strongly to pick up a morale-boosting victory.
Owen Lane and Hallam Amos scored Cardiff's tries with Evans adding four penalties and two conversions with Ben Thomas kicking a penalty.
Adam Byrne, James Tracy and Scott Penny were Leinster's tryscorers with Ross Byrne kicking two penalties and three conversions.
A penalty from Byrne gave Leinster an early lead but that was the only score of a keenly-contested opening quarter.
The visitors had marginally the better of that period with their more-accurate passing and creative running but both defences held firm.
A drive from the Cardiff pack won a penalty for Evans to succeed with a 40-metre kick before the outside-half’s superbly-judged kick was collected by Lane, who sped past two defenders for an excellent solo try.
The Irish response was immediate as Adam Byrne raced away to score with Ross Byrne’s conversion bringing the scores level.
Evans temporarily left the field for an HIA and in his absence, Thomas regained the lead for his side with a straightforward penalty.
Leinster suffered an injury blow when their flanker Will Connors was forced to leave the field and at half-time they were still trailing 13-10.
Evans returned for the second half and almost immediately kicked his second penalty as his side continued to cause the visitors problems in the scrum and at the breakdown.
Cardiff had certainly come out firing after the interval and deserved to extend their advantage with another Evans penalty.
Leinster showed their fighting spirit with a close-range try from Tracy after Cardiff’s Botham was shown a yellow card for dragging down a lineout drive.
Botham was still absent as the lead changed hands again when Penny broke away from a lineout, 15 metres out to score.
Botham returned in time to see Cardiff grab a lifeline with a stroke of good fortune.
Harry Byrne’s penalty attempt from inside his own half rebounded back off the crossbar for Cardiff to launch a clearance.
Byrne, believing the ball to be going straight into touch, left it to bounce for Aled Summerhill, who raced into the opposition 22 from where Amos scored against a flat-footed defence.
Leinster looked to have won it when Ross Byrne fired over a penalty but Evans had the final say.