The Bluebirds came from behind twice to eventually make it three wins in a row under new manager Neil Harris.
Conor Chaplin's early strike was followed by messy goals for each team, claimed by Aden Flint and Bambo Diaby, as Gerhard Struber's men stayed on course to lift themselves off the bottom of the Championship.
But Danny Ward came off the bench and equalised before Tomlin completed the comeback victory deep into injury time to snatch all three points.
Harris named an unchanged side for the third straight game as he looked to build on his unbeaten start as Cardiff manager.
Barnsley also stuck with the same starting XI from the 3-1 triumph over Hull that ended an 18-game winless run that stretched all the way back to the opening day of the season.
The Tykes started brightly on the back of their morale-boosting win, but were almost caught with a sucker punch after Flint intercepted the ball and charged out from defence.
The centre-back laid the ball off and continued his run to make space for Junior Hoilett to cut in and curl a left-footed effort inches past the post.
Despite almost being caught on the counter attack, the visitors reasserted their early dominance and took the lead moments later.
Mike Bahre picked up possession and slipped a through-ball behind Cardiff defender Curtis Nelson and into the path of Chaplin, who slid a perfectly placed shot into the bottom corner for a 1-0 lead after 17 minutes.
But the advantage lasted less than three minutes as the Bluebirds got back on level terms. Junior Hoilett whipped in a cross, with Flint claiming the goal after challenging with Diaby.
Chaplin came close to a second just before half-time when he steered Cauley Woodrow's cross just wide, but the teams went in level.
Parity only lasted for two second-half minutes thanks to Barnsley defender Diaby's first goal for the club.
The Cardiff defence was slow to close Chaplin down after he picked up a loose ball. The forward fired in a cross that hit Diaby on the side, with Lee Peltier in attendance, and went past Neil Etheridge and in.
But Ward was introduced from the bench and made a near instant-impact to equalise on 68 minutes.
Tomlin won the ball off Ben Williams and carried it across midfield before playing a through-ball past Diaby. Ward raced onto the pass and lifted the ball over Sami Radlinger to give Cardiff hope of a comeback victory, which they looked to have spurned with two late misses.
Flint headed the ball back into the danger zone at the far post for Tomlin to volley home the winning goal.