The victors only went ahead in the final two minutes but even then, Caerphilly, who led 4-2 on try-count, had two penalty chances to send the game into extra time, but first fly half Dan Ellis, right on time, and then full back Cody Rees, in injury time, missed with their attempts.
Caerphilly took an astonishing first-half lead with centre Neil Dallimore a threat every time he was near the ball.
Ellis put Dallimore away and he fed No 8 Ceri Jones for the opening try that Rees converted. Then, a Dallimore chase and kick ahead brought him a try and just before the interval Dallimore and scrum half Alun Ford helped Jones go over again with Rees converting.
However, Dallimore was injured in a completely accidental collision which saw Bargoed hooker Luke Harwood taken off badly shaken.
After 47 minutes Bargoed finally got on the scoresheet when lively scrum half Matthew Hutcheon scored and three minutes later centre Rhys Humphries raced clear with fly half Jamie Howells goaling both.
Caerphilly responded with a superb try as Ellis broke and wing Stefan Sankala went between the posts for Rees to convert at 26-14.
Then, Howells took a hand as he fired over penalties in the 65th, 70th, 72nd and 75th minutes to level the scores and in the 78th minute Hutcheon fed Howells, who cooly dropped a goal.
Caerphilly were still not finished and John Griffiths powered them into scoring positions, but both Ellis and Rees missed badly with their penalty efforts.
Starting lineups
Bargoed – I Jenkins; G Hodges, D Humpries, R Humphries, L Pearson; J Howells, M Hutcheon; R Jones (capt), L Harwood, R Young, H Davies, A Kidley, A Cullen, L Wayman, G McCarthy.
Caerphilly – C Rees; E Lewis, L Mullane, N Dallimore, S Sankala; D Ellis, A Ford; J Greenfield, J Walby, A Richards, J Benfield, C Payne (capt), J Griffiths, C Jones, G Johns.
Referee – David Williams (Caldicot).