It has an average annual value of $3.25 million. The 23-year-old defenseman could have become a restricted free agent July 1.
"I'm obviously really excited," Sanheim said. "It's a big step in my career. I'm looking forward to another two years with the Flyers. I'm really excited with the way the team's moving forward and the moves we've made this summer."
Sanheim had NHL career highs in goals (nine), assists (26) and points (35) in 82 games this season. Selected by the Flyers in the first round (No. 17) of the 2014 NHL Draft, he has 45 points (11 goals, 34 assists) in 131 regular-season games and one goal in four Stanley Cup Playoff games.