The 24-year-old defenseman could have been a restricted free agent July 28.
"Feeling over the moon today," Carlo said. "This is a huge day for me and my family."
Carlo scored four points (three goals, one assist) in 27 regular-season games, missing time because of a concussion and an upper-body injury. He did not score a point eight Stanley Cup Playoff games but his average ice time of 22:30 per game was second on the Bruins to defenseman Charlie McAvoy (26:39).
Carlo said he has felt healthy for "well over a month" and will resume skating next week.
"I never felt, in any way, shape, or form, that my career was going to be ending any time soon," he said. "I think, just going through all of these situations, I've learned more and grown a lot from it. From the focal point of if it was going to affect me going forward, I don't feel that way. I'm still a young guy, I still feel very sharp in my mind. I feel great in my body. That was not a concern of mine, no."
The Bruins lost to the New York Islanders in six games in the best-of-7 Stanley Cup Second Round, the second consecutive season they were eliminated from the playoffs in the second round.
Carlo was selected by the Bruins in the second round (No. 37) of the 2015 NHL Draft. He has scored 55 points (15 points, 40 assists) in 324 regular-season games and five points (two goals, three assists) in 45 postseason games. Since 2016-17, Carlo is second on the Bruins in total shorthanded time on ice (908:57), fourth in games played (324), fifth in time on ice per game (20:13) and tied for fifth in plus/minus (plus-58).
"The Bruins are very pleased to have extended Brandon on a long-term deal," general manager Don Sweeney said. "Brandon is a player who has grown into a foundational defenseman with our team while also emerging as an important leader on and off the ice."
Forwards David Krejci and Taylor Hall, and goalie Tuukka Rask each can become an unrestricted free agent July 28. Rask will be sidelined until January or February because of surgery to repair a torn labrum in his hip. He has said he only wants to play for Boston.
Defenseman Kevan Miller
retired from the NHL
on Wednesday. Forward Trent Frederic signed a two-year, $2.1 million contract June 25.
"I'd like to congratulate Kevan Miller," Carlo said. "Probably the guy that I've grown closest with the past couple of years. He's been a huge mentor of mine. We've had many conversations and coming to the league, being a right-handed defenseman and playing in similar roles, he did such a good job handling me and helping me along the way."
NHL.com staff writer Mike Zeisberger contributed to this report