Pittsburgh increased its lead for the NL's top wild card to 4 1/2 games over the Cubs, who would have secured their first postseason appearance since 2008 with a victory. Chicago would clinch with a loss by San Francisco to Oakland on Friday night.
Cole (18-8) and Lester (10-12) each lasted seven innings, and Mark Melancon held off a rally attempt in the ninth for his major league-leading 51st save in 53 chances.
Cole gave up two runs and five hits, struck out eight and walked two while improving to 7-1 in nine starts against Chicago.
He also drove in the game's first run with a single in the third.
Michael Morse put Pittsburgh ahead 2-1 with an RBI single off Lester in the seventh. The Pirates added a run in the eighth against Pedro Strop when Jordy Mercer reached on a wild pitch as he struck out leading off and scored from third on a grounder by Starling Marte.
Lester allowed two runs and five hits.
Kris Bryant had two hits and scored, but Chicago came up short after winning seven of nine.
The Cubs tied it in the fourth when Bryant led off with a single and scored from third on a force by Addison Russell.
It stayed that way until the seventh, when former Cub Aramis Ramirez led off with a double and scored on Morse's one-out single. Morse, who was running on the pitch, then got doubled off on Josh Harrison's pop fly to first to end the inning.
Pirates: RF Gregory Polanco (knee) and 3B Ramirez (groin) were back in the lineup after being held out Thursday against the Colorado Rockies.
Pirates: LHP Francisco Liriano (11-7, 3.41 ERA), coming off a win over Clayton Kershaw and the Los Angeles Dodgers, looks to stay on a roll for Pittsburgh. He is 7-1 in his past 15 starts, with the Pirates winning all but one of those games.
Cubs: With a 5.77 ERA over his last 11 starts, RHP Jason Hammel (9-6, 3.79) looks to break out of his slump.