The on-loan Wolves striker, who otherwise had a quiet afternoon, picked the perfect moment in the 89th minute to grab his first goal of the season and banish memories of back-to-back defeats for the Championship promotion hopefuls.
City were anything but impressive, though, and the result was harsh on a Millwall side who had Liam Trotter sent off for a bad foul on Darcy Blake after 40 minutes and had David Forde to thank for producing a superb save to keep out Peter Whittingham's penalty.
Lions defender Scott Barron had handed the visitors an early lead with a rare strike, only to see Jay Bothroyd equalise with his fifth goal of the season midway through an eventful first half.
The second period failed to live up to the same billing with Cardiff lacking inspiration and Millwall working tirelessly, but it was the hosts who snatched the points in dramatic fashion to leave Kenny Jackett's side now winless in five games.
Cardiff, still without the injured Craig Bellamy, Michael Chopra, Danny Drinkwater and Seyi Olofinjana, struggled to get into their rhythm in the opening quarter, giving possession away cheaply.
And Millwall capitalised fully on their sloppy play when they broke the deadlock after just 10 minutes.
Kevin Lisbie found Barron in a good position and he fired low past Tom Heaton from the left-hand side of the six-yard box.
The visitors continued to hold the upper hand until the home side equalised in the 24th minute.
Lee Naylor's long throw found Whittingham and his stab goalwards was blocked as far as Bothroyd, who duly found the bottom corner from close range.
That was the first of what could have been a quickfire hat-trick for the Bluebirds frontman, although Lisbie and Trotter were both giving the Cardiff rearguard plenty to think about at the other end.
However, Jackett was soon forced to reconsider his side's approach when Trotter received a straight red five minutes before the break.
But he was spared further half-time headache in injury time when Forde produced a great save to keep out Whittingham's penalty after he had been brought down by Darren Ward.
Cardiff failed to take advantage of their numerical superiority in the opening 20 minutes of an ordinary second half, with Millwall working hard and looking more likely to add to the scoreline.
That prompted Dave Jones to introduce Jason Koumas which certainly put City on the front foot, Mark Hudson seeing his header blocked by Paul Robinson and Whittingham blasting over.
Neither Forde nor counterpart Heaton had been forced to make a single second-half save as the game approached the final 15 minutes.
But as the clock ran down Cardiff forced the issue and Keogh was on hand to head home Bothroyd's cross.