The hosts had looked set to move a step closer to the Barclays 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 Tuesday night's Championship match at the Cardiff City Stadium.
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 Cardiff's bright opening in the 18th minute - Luke Steele diverting his shot against the post after the Wales international retrieved Smith's fizzing ball across the box.
Marlon Harewood had the ball in the net but was correctly called back for offside, and then Steele made an excellent save to tip Jordon Mutch's stinging drive round the post.
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.