The hosts had looked set to move a step closer to the Premier League when centre-half Ben Turner guided home Kim Bo-Kyung's corner just before the hour mark.
The Welsh side also hit the post through Craig Bellamy but they were punished for failing to capitalise on their dominance when Foster's deflected shot trickled over the line with what was effectively the last kick of the match.
Despite the setback, Cardiff's promotion still appears a formality as they now lead third-placed Watford by nine points with five games remaining.
Barnsley, though, will not care about spoiling the party after stealing a point which moves them two clear of the drop zone, despite being penned in their own half for much of the game.
The tone of the contest was set during a confident start from the hosts, which saw Tommy Smith, Bellamy and Aron Gunnarsson all got away shots on goal, although Luke Steele was only required to save the first of those efforts.
Bellamy came close to rewarding the bright opening in the 18th minute - Steele diverting his shot against the post after the Wales international retrieved Smith's fizzing ball across the box.
Barnsley were happy to drop deep and look to hit on the break, but it took almost half an hour for them to threaten the Cardiff goal; Marlon Harewood had the ball in the net but was correctly called back for offside.
Jordan Mutch, Kim and Gunnarsson were enjoying the run of midfield, and Steele made an excellent save to tip the former Birmingham man's stinging drive round the post.
The midfield trio linked beautifully again on 37 minutes, and it took a brave Foster block to deny Gunnarsson as he prepared to pull the trigger.
The extent of Cardiff's dominance was such it appeared a case of when, not if, they would open the scoring, but the Tykes gave them a real scare early in the second half.
The Bluebirds were slow to react to a short corner and Bobby Hassell just failed to make a telling contact with David Perkins' pin-point cross.
Cardiff responded to the wake-up call and a lovely touch from Mutch set up Kim for the Korean to fire across goal, while Leon Barnett could not adjust in time after Steele punched poorly in his own six-yard box.
The pressure had to tell, and the goal finally came from an unlikely source.
Kim whipped a dangerous corner into the six-yard box and Turner stuck out a leg to guide the ball just inside the post for his first goal of the season.
The lead was almost doubled three minutes later. Mutch had a shot blocked by Hassell, while Barnett and Gunnarsson had efforts cleared off the line from the resulting corner.
With a single-goal lead, Cardiff were far from out of sight, and Harewood spurned Barnsley's best chance of the night with a tame effort from Stephen Dawson's astute pass.
Substitute Heidar Helguson wasted a wonderful chance to wrap up the three points in stoppage time, and it proved costly as Foster's shot took a big deflection to leave David Marshall stranded and level the scores.