Fernandez (7-5) allowed five hits and two runs in eight innings. The 20-year-old All-Star right-hander walked none and threw 97 pitches, 74 for strikes.
Cole (5-4) was nearly as good. The rookie allowed three runs and struck out eight in seven innings, but Giancarlo Stanton put the Marlins ahead to stay in the sixth with his 13th home run.
Stanton also doubled, walked and scored twice for the Marlins, who had six hits.
Steve Cishek gave up a single and a walk in the ninth, but retired Garrett Jones on a grounder for the final out to earn his 22nd save in 24 chances.
The Marlins have the worst record in the NL but took two of three games from the Pirates, who began the day with the second-best record in the majors.
Pittsburgh went 5-5 on a three-city trip and begins a potentially pivotal five-game series at home against NL Central leader St. Louis on Monday.
Fernandez's strikeout total broke the Marlins' rookie record of 12 held by Josh Beckett. He struck out every player in the starting lineup, and he fanned the side in the third and fifth innings.
Fernandez improved to 5-2 with a 1.87 ERA over his past 10 starts, and lowered his ERA for the season to 2.71.
When his face was shown on the video board before the ninth inning, the crowd of 24,207 roared, and he waved his cap from the dugout.
The Pirates scored their only runs in the second inning. Pedro Alvarez led off with a single and came home on Russell Martin's double. Martin stole third and scored on a two-out single by Clint Barmes.
Miami made it 2-all in the fourth when Stanton walked, Logan Morrison singled, Ed Lucas singled home a run and Donovan Solano hit a sacrifice fly.
Notes: Pirates C Michael McKenry, who tweaked his left knee Saturday, was awaiting results of a doctor's evaluation. C Russell Martin was back in the lineup after suffering a minor injury to his left knee Friday. ... Pittsburgh RF Jose Tabata started after being held out of the starting lineup for three games because of a sore right forearm. ... Pirates CF Andrew McCutchen was given a rest. He pinch-hit in the eighth and popped out. ... Before the game, RHP Steven Ames was recalled by the Marlins from Triple-A New Orleans. Ames was part of the July 6 trade that sent RHP Ricky Nolasco to the Dodgers. ... Miami RHP Kevin Slowey, who went on the disabled list Saturday after complaining of forearm tightness, was scheduled to undergo an MRI on Monday. ... The Marlins have beaten the Mets in five consecutive games heading into the start of their four-game series Monday.