Leeds had led the game for the whole of the second half and looked odds-on to move into the top half of the Zurich Premiership table.
But Barkley's try levelled the scores with the last play of the game and allowed Chris Malone to kick the winning conversion.
It was a cruel blow for Leeds who had played with spirit and aggression throughout, but ultimately they were left to rue some naive decision-making in the final period.
The game had exploded into life when Leeds openside Dan Hyde was sin-binned in the first minute for over enthusiastic use of the boot. Barkley missed the ensuing penalty but Bath remained camped in the Leeds 22.
Bath won a lineout 10 metres from the line and the forwards turned on the power, driving forward and across, until Rob Fidler forced his way over the line, under the posts.
Referee Sean Davey awarded the try after consulting the video referee and Barkley converted.
The home side responded through a mammoth Tim Stimpson penalty after Leeds had infringed at the breakdown and Stimpson's second penalty after 17 minutes made the score 6-7 to the visitors.
Barkley kicked another penalty after 21 minutes when Bath took their turn to infringe at the ruck.
Leeds crossed the Bath line after Stimpson made a terrific break up the right touchline.
The Tykes managed to keep possession despite repeated attempts by Bath to slow the ball down.
The Leeds forward powered on before their powerful centre Chris Bell stretched for the line to score. Stimpson converted to make it 13-10 to the home side at half-time.
Bath came out of the blocks quickly in the second half and scored a fine try after 42 minutes through Barkley.
Fly-half Chris Malone slipped through a weak Jon Dunbar tackle before drawing Stimpson and slipping a deft pass to Barkley to go in under the posts. Barkley converted his own try.
Malone was involved again three minutes later when he struck a sweet drop-goal from 45m to make it 20-13 to the visitors.
Bath had flanker James Scaysbrook sin-binned after 48 minutes for pulling down at the back of the line-out, and Leeds made them pay with a well-worked try for South African centre Andre Snyman. Stimpson missed the conversion which would have levelled the scores.
Tykes struck again with Scaysbrook off the pitch when Jon Dunbar was driven over from a catch and drive at the line-out. Stimpson's conversion from out-wide made it 25-20 to the Tykes.
Another Stimpson penalty after 60 minutes gave the home side an eight-point advantage.
Bath pressed hard with Leeds making a series of basic errors in trying to clear their lines. A Malone penalty after 68 minutes set up a tense finish with five points between the sides.
The visitors looked like they like might have stolen it after 70 minutes but the video referee ruled against them after a fierce drive from the line-out.
However, it was not too late for Bath, as Barkley finally found a gap in the Leeds defence to break the home fans' hearts.
For Leeds Tykes: Tries: Dunbar, Bell, SnymanCons: Stimpson 2Pens: Stimpson 3
For Bath:Tries: Fidler, Barkley 2Cons: Barkley 2Pens: Malone, BarkleyDrops: Malone