Ben Cabango struck a last-gasp winner as Swansea City made more south Wales derby history by recording a fourth-straight victory in the fixture at the Cardiff City Stadium.
The Swans, who had won the previous three derbies without conceding, swept into an early lead when Joel Piroe fired home from the first meaningful attack of the contest at Cardiff City Stadium.
Liam Cullen headed home a rebound for the second in the 34th minute, but any thoughts of a comfortable afternoon when Jaden Philogene immediately responded with a solo goal for the home side.
The second half was an altogether more cagey affair, but the hosts looked set to secure a point when Sory Kaba headed them level with seven minutes to go.
But Cabango scrambled the ball home after a Jay Fulton strike had cannoned back off the post in the final seconds of stoppage time.
The Swans had fielded an unchanged XI from the side that started the win over Bristol City, and also had Joe Allen, Fulton and Joel Latibeaudiere on the bench as they returned from injury and suspension respectively.
The hosts made three changes as Joe Ralls, Andy Rinomhota and Sheyi Ojo came into the starting line-up.
Swansea had enjoyed a superb start in this fixture last season, and they replicated it by leading with barely two minutes on the clock.
The Cardiff defence were caught sleeping from a long throw, and Piroe twisted and turned away from Mark McGuinness to smash a right-footed shot high into the net.
Cardiff responded, Callum O’Dowda getting to the byline and firing a low cross into the area, which somehow evaded all blue and white shirts.
Another big chance fell Swansea’s way when Piroe again escaped McGuinness. The Dutchman’s cross was cut out and fell to Olivier Ntcham, but his follow-up effort was blocked before Harry Darling blasted an effort wide from distance.
Liam Walsh nearly threaded Piroe in behind a few minutes later, but McGuinness read the danger and beat the Swansea forward to the ball.
At the other end Nathan Wood made an important block after Jaden Philogene made a positive surging run down the Cardiff right.
Sheyi Ojo then forced Andy Fisher into his first save of the afternoon with a strike from the edge of the box after a purposeful run.
The hosts were having a better spell, and Wood and Ryan Manning made two crucial clearances in the area before O’Dowda could not turn a scuffed volley goalwards.
Swansea still carried a threat, Ryan Allsop having to be off his line quickly to deny Liam Cullen as he ran on to Luke Cundle’s attempted pass.
But the striker doubled Swansea’s lead 11 minutes before the break.
Manning sent Piroe in behind once again, and while a combination of Allsop and the post kept his effort out, Cullen was on hand to stoop and head home the rebound.
But Cardiff responded with a superb goal from Philogene, who picked up a loose ball and beat two men on the edge of the area before firing a rising shot that Fisher got hands to but could not keep out of the net.
That set up a strong end to the opening period for the hosts but the Swans retained the lead at the interval.
Both sides made changes at the interval; Fulton replacing Cundle for the Swans and Romaine Sawyers coming on for O’Dowda.
The game remained an open affair, with Manning having a dangerous cross blocked while Darling could not keep the ball in after a flowing Swansea attack.
Swansea had regained a measure of control but a quick spate of bookings for Ntcham, Fulton and Ollie Cooper got the crowd up and Cardiff enjoyed the chance to throw the ball into the Swansea box just before the hour.
Swansea could not make the most of a big chance seconds later, Cullen getting in behind and squaring for Piroe. The Dutchman rounded Allsop, but his touch had taken him wide and he could not keep it in.
Into the final 20 minutes and the tension was palpable, with Fisher quickly off his line to prevent a through ball finding Sory Kaba in the area.
The Swans were forced into a reshuffle as injury to Darling saw Jamie Paterson introduced and Cooper move to wing-back for the final 15 minutes.
Cardiff began to throw more numbers forward and they levelled matters when Kaba rose to head home after Rubin Colwill had dug out an excellent cross from the byline with seven minutes to play.
There was still time for more drama. Matt Grimes thought he had won it with a superb volley which had Allsop beaten, but cannoned back off the crossbar.
However, when Swansea next struck the woodwork, Cabango was on hand to find the net at this ground for the second season in a row.
Swansea City: Andy Fisher; Harry Darling (Jamie Paterson 76), Nathan Wood, Ben Cabango, Ryan Manning; Liam Walsh (Joe Allen 64), Matt Grimes (captain); Luke Cundle (Jay Fulton 46), Olivier Ntcham (Ollie Cooper 52), Joel Piroe; Liam Cullen (Joel Latibeaudiere 64).
Substitutes: Andreas Søndergaard, Jamie Paterson, Morgan Whittaker.
Cardiff City: Ryan Allsop, Mark McGuinness, Ryan Wintle, Joe Ralls (captain), Sheyi Ojo (Connor Wikcham 61), Callum O'Dowda (Romaine Swayers 46), Cedric Kipre, Jaden Philogene, Andy Rinomhota (Rubin Colwill 61), Perry Ng (Mahlon Romeo 90), Sory Kaba.
Substitutes: Jak Alnwick, Jack Simpson, Isaak Davies.
Referee: Keith Stroud