Evgeni Malkin is staying in Pittsburgh after all.

The Penguins and the Russian star agreed to a four-year contract late Tuesday night worth $6.1 million annually. The deal was signed just hours before the three-time Stanley Cup-winning center was set to become a free agent for the first time in his career.

CLICK HERE FOR MORE SPORTS COVERAGE ON FOXNEWS.COM

Evgeni Malkin celebrates goal

Evgeni Malkin #71 of the Pittsburgh Penguins reacts after scoring a goal in Game Six of the First Round of the 2022 Stanley Cup Playoffs against the New York Rangers at PPG PAINTS Arena on May 13, 2022 in Pittsburgh, Pennsylvania.  (Photo by Kirk Irwin/Getty Images)

Instead, the 35-year-old Malkin will remain alongside longtime teammates Sidney Crosby and Kris Letang. The Penguins signed Letang to a six-year contract last week, but progress on a new deal with Malkin appeared to be stalled as late as Monday, when a person with knowledge of Malkin's thought process told The Associated Press that Malkin intended to test the open market.

What appeared to be a potentially bitter divorce with the start of free agency looming on Wednesday instead turned into an 11th-hour deal. The 2012 Hart Trophy winner and two-time league scoring champion will remain with the team that took him with the second overall pick in the 2004 draft.

"Evgeni is a generational talent who will be remembered as one of the greatest players in NHL history," Penguins general manager Ron Hextall said in a statement. "His hockey resume and individual accomplishments speak volumes about him as a player, and we are thrilled to watch him continue his remarkable legacy in Pittsburgh."

Pittsburgh's core of Malkin, Letang and Crosby will remain together through at least 2025, when Crosby’s deal expires. Crosby made it clear that he wanted the Penguins to find a way to keep both of his longtime running mates, with Hextall allowing that " in a perfect world " the club would find a way to make it work.

NHL FREE AGENCY: THE LATEST ON JOHNNY GAUDREAU, NAZEM KADRI, AND MORE

Evgeni Malkin celebrates goal

Evgeni Malkin #71 of the Pittsburgh Penguins reacts after scoring a goal during the second period in Game Six of the First Round of the 2022 Stanley Cup Playoffs against the New York Rangers at PPG PAINTS Arena on May 13, 2022 in Pittsburgh, Pennsylvania.  (Photo by Kirk Irwin/Getty Images)

At the last minute, the planets apparently aligned.

The Penguins are banking on Malkin returning to form as he enters his late 30s. Malkin missed half of last season while recovering from knee surgery. He scored 20 goals to go with 22 assists in 41 games and added another three goals and three assists in Pittsburgh's first-round playoff series loss to the New York Rangers.

Malkin has played more than 70 games in a season just once since being named MVP in 2012.

Still, when engaged he remains a potent scorer, particularly on the power play. The immensely popular Malkin has long thrived alongside Crosby. He is in the top five in most major statistical categories in franchise history, including games (981), goals (444), assists (702) and points (1,146).

The deal follows several re-signings by the Penguins, who have reached the playoffs 16 straight seasons but haven't gotten past the first round since 2018.

In addition to bringing back Letang on a six-year deal, Pittsburgh signed forwards Bryan Rust and Rickard Rakell to lengthy extensions.

CLICK HERE TO GET THE FOX NEWS APP

Kris Letang and Evgeni Malkin celebrate a goal

Kris Letang #58 of the Pittsburgh Penguins (R) celebrates his second period goal against the New York Rangers and is joined by Evgeni Malkin #71 (L) in Game Five of the First Round of the 2022 Stanley Cup Playoffs at Madison Square Garden on May 11, 2022 in New York City.  (Photo by Bruce Bennett/Getty Images)

Malkin's signing means the Penguins won't have much money to spend on the open market as they try to keep their Stanley Cup window open with their aging core.

Bringing back Malkin indicates Hextall and president of hockey operations Brian Burke believe there's enough left in Malkin's tank to keep the Penguins in the mix in the Metropolitan Division.