St. Louis led 2-1 entering the eighth.
But Gallegos walked a batter, then hit one, before Trea Turner’s RBI single to right on an 0-2 count tied the game.
Then came the free pass to Gomes.
Daniel Hudson earned the win. Brad Hand got the save.
Adam Wainwright struck out 10 in seven innings for the Cardinals.