SAN FRANCISCO -- It took an incredible rally to end an incredible streak.
As a result, the Chicago Cubs' incredible season marches on.
Javier Baez capped a four-run rally in the top of the ninth inning with a run-scoring single Tuesday night, lifting the Cubs to a 6-5 victory over the San Francisco Giants and a four-game win in the best-of-five National League Division Series.
By denying the Giants an 11th consecutive win in a postseason elimination game, the Cubs move on to their second consecutive NL Championship Series, beginning Saturday at Wrigley Field.
The Cubs haven't won the NL title since 1945, and they haven't won the World Series since 1908.
"I think it validates on a lot of different levels," Cubs manager Joe Maddon said of the series win. "If you're a player on this particular team within the organization, it's getting to the point now you want to expect to get to the postseason, and you want to expect to get deeply into the postseason."
The Giants, a wild-card entry into the playoffs, were bidding to continue their even-year success that saw them win World Series championships in 2010, '12 and '14.
"It's a weird feeling." Giants manager Bruce Bochy said. "It kind of gives you an empty stomach to go out like this.
"It's a tough way to go out, but what a great job (our team) did, going back to the last homestand, (the) wild-card (game), and these were hard-fought games. Give the Cubs credit."
The Chicago uprising came against five San Francisco relievers after Giants starter Matt Moore limited the Cubs to two hits over eight innings.
With the Cubs down 5-2, the dramatic rally began when Kris Bryant grounded a leadoff single off right-hander Derek Law.
Ben Zobrist greeted Romo with a double into the right field corner, scoring Bryant and putting the tying runs in scoring position with still no outs.
"We don't quit. That's really what it comes down to," Maddon said. "You hear that all the time, everybody says it, but you have to actually live it."
The Cubs weren't done simply tying the game.
With still no outs, Jason Heyward was called upon to sacrifice, and he bunted back to the mound. Smith got the lead runner at second, but Brandon Crawford threw wildly to first base, allowing Heyward to reach second.
The error was Crawford's second of the game.
Baez then singled up the middle against the Giants' fifth pitcher of the inning, Hunter Strickland, to score Heyward with the eventual game-winner.
"He did his job," Bochy responded when asked if he considered allowing Moore to pitch the ninth after having thrown 120 pitches. "I would like to think you're going to get three outs there. We couldn't do it."
Aroldis Chapman, who blew a save Monday in the Cubs' 13-inning loss, struck out the side in the ninth to record his third save of the series, sending the Cubs into the next round.
The win was the third in Cubs postseason history when trailing after eight inning, and was just the second in major league history for a team trailing by three or more runs entering the ninth of a potential close-out game.
"The game of baseball is a game that is 27 outs," Baez said. "We can't give up because we're down. We were fighting and fighting ... We just made their bullpen pay."
Hector Rondon (1-0), who worked a scoreless eighth inning, got the win. Smith (0-1) was credited with the loss, even though the run off him was unearned.
Facing the Cubs for the first time in his career, Moore allowed a third-inning solo home run to catcher David Ross and an unearned run in the fifth en route to his longest start of the five in his personal postseason history.
The in-season acquisition from the Tampa Bay Rays struck out 10, his postseason best.
Batting sixth and seventh, respectively, they combined for six hits, two RBIs and two runs.
Gillaspie collected four singles, and Panik and Denard Span had two hits apiece for the Giants, who were attempting to duplicate their rally from a 0-2 deficit against the Cincinnati Reds in the 2012 NL Division Series.
The win for the Cubs was just their fourth in nine potential close-out games in their postseason history.
Gillaspie's second and third hits came in consecutive two-run uprisings as the Giants broke away from a 1-1 tie and into a 5-2 lead through five innings.
After Ross' homer in the third inning offset a Giants run in the first, Gillaspie got the two-run fourth rolling with a single.
Panik followed with his second hit, and a Gregor Blanco walk loaded the bases.
Down 0-2 in the count, Moore grounded his RBI single into right field to give the Giants the lead. Span's run-scoring infield out one batter later made it 3-1.
Lackey was pulled at inning's end, having allowed three runs on seven hits. He walked two and struck out four.
Gillaspie's hit in the fifth produced the inning's first run when it plated Hunter Pence, whose baserunning gaffe temporarily denied the Giants the run.
After Pence led off the inning with a single, Crawford launched a high fly off the concrete railing atop the 20-foot-high wall in right-center field.
Pence, who was watching the near-homer, overstepped second base after the ball bounced back into play. He had to retrace his steps, then could only reach third base on the double.
Turns out, it didn't matter. Gillaspie's single and Panik's sacrifice fly brought both baserunners around, increasing the lead to 5-2 against the Chicago bullpen.
NOTES: The Cubs will be making back-to-back appearances in the National League Championship Series for the first time in their history. ... Chicago's David Ross, 39, became the oldest catcher to hit a home run in a postseason game. ... The loss when leading after eight innings was the first in Giants postseason history since 1911. ... SS Brandon Crawford was the first Giant to commit two errors in a playoff game since RF Don Mueller in 1954. ... Cubs RHP John Lackey made his 21st postseason start, which tied him with RHP Mike Mussina for seventh most in major league history. LHP Andy Pettitte holds the record with 44. ... The record-holders for most consecutive wins in potential elimination postseason games in the NBA and NFL are the Boston Celtics (11, 1959-67) and New England Patriots (10, 2002-06).