The Luxembourg midfielder tucked the ball home from the edge of the box in the third minute of added time, sending his elated teammates cascading from the bench.
The victory - Mainz's first this season against a team in the bottom four and the first in Cologne in its history - sent it three spots clear of the automatic relegation places.
Mainz winger Jean-Paul Boetius opened the scoring after 11 minutes, but Cologne drew level before half-time after being awarded a penalty following a handball by Mainz defender Phillipp Mwene.
Ondrej Duda squeezed his spot kick past Robin Zentner for his fifth goal of the season.
Cologne took the lead in the 61st minute after Ellyes Skhiri headed home a cross from Jonas Hector, but Mainz hit back quickly through Karim Onisiwo to make it 2-2, before Barreiro scored late to win it for the visitors.
The win continues Mainz's remarkable resurgence under new manager Bo Svensson, who took over in January with the side in 17th place on six points.
Mainz has collected 22 points in the 14 games since, fifth most in the Bundesliga over that period, to drag itself out of the relegation zone.
Svensson said after the match that he would hold off celebrations until the job was done.
"We're not even close to through yet. We've got a difficult programme with three games in six days which is a huge challenge and we'll need to use the entire squad," he told Sky Sport.
With his side in second-last spot and at least three points from safety, Cologne manager Markus Gisdol addressed speculation this game may be his last in charge of the Billy Goats.
"I'm going back into the dressing room and then I'll do the press conference, and then I guess we'll see."
In the early game, a goal from birthday boy Suat Serdar secured Schalke a 1-0 home victory over Augsburg - just the second league win of the season for a side all but condemned to relegation.
Serdar was able to pounce on a rare mistake from Augsburg goalkeeper Rafal Gikiewicz in the fourth minute, poking the ball home after the Pole spilled a tame cross from Can Bozdogan.
Schalke manager Dimitrios Grammozis, celebrating his first victory since taking the reins of the side mid-season, described the feeling as one of "extreme happiness".
"It is probably a feeling as if you are in love for the first time," he said.
The victory saw Schalke move up to 13 points and avoid a number of unwanted records, including fewest points (10) and fewest wins (one) in a Bundesliga season, both of which remain the property of Tasmania Berlin from the 1965-1966 campaign.