ST. PETERSBURG — Pinch-hitter Ben Williamson bounced a walk-off single through the right side in the bottom of the 12th inning Tuesday night at Tropicana Field, lifting the Tampa Bay Rays to a 4-3 victory over the Milwaukee Brewers in Game 4 of the World Series and forcing a Game 5.

The win kept the Rays alive after falling into an 0-3 series hole, making them just the 11th team in 44 to force a fifth game after dropping the first three in a best-of-seven. Only the 1910 Cubs had previously pulled off a walk-off win facing a sweep in the World Series.

“Tonight, good things happened,” Williamson said.

The Rays needed all 12 innings to get there. Brewers rookie shortstop Cooper Pratt hit a go-ahead solo home run in the top of the 12th to give Milwaukee a 3-2 lead and push the Brewers within three outs of their first championship — and the first team ever to sweep both a best-of-seven LCS and World Series. “That was probably one of the coolest swings of my life,” Pratt said. “But it’s over.”

It wasn’t. Brewers third baseman Joey Ortiz committed a costly error on a Junior Caminero grounder, allowing Caminero to reach base. A series of singles followed — Liam Hicks through the left side, Jonny DeLuca up the middle to score pinch-runner Cedric Mullins with the tying run, Chandler Simpson to center to load the bases — before Williamson’s game-winner ended it. “We weren’t knocking balls in the gap or knocking balls out of the ballpark,” manager Kevin Cash said. “We pieced together a big inning, maybe the most important inning of our season.”

The rally also snapped Milwaukee’s eight-game postseason winning streak, one shy of a new record. Brewers captain Christian Yelich acknowledged the difficulty of the moment without conceding the series. “If you want to win a championship, you’re supposed to survive some hard things,” Yelich said. “It’s supposed to be hard. It’s supposed to test you.”

The Rays summoned would-be Game 5 starter Nick Martinez for two scoreless relief innings in the marathon contest. Brewers reliever Aaron Ashby extended what has become a record 18 straight scoreless postseason innings of relief. Earlier, Brewers third baseman David Hamilton had missed a tag on Caminero and thrown errantly to first in the fourth inning, allowing Caminero to score what proved to be a critical run.

There had been 16 previous walk-off wins in World Series history by a team facing elimination — 13 of them in extra innings, and 4 in the 12th inning or later. Before Game 4, Rays president of baseball operations Erik Neander had set a modest goal. “We need to win an inning first,” Neander said. He got 12.

Game 5 is scheduled for Wednesday night at Tropicana Field, where the Rays will try to become just the fifth team in baseball history to come back from an 0-3 deficit to force a Game 6 — and the first ever to do so in the World Series.