Kilgore, who was scheduled to become an unrestricted free agent on March 14, expressed his excitement with his new deal and the direction of the team.
"I have been here for seven years, and I consider the Bay Area my second home and to be able to extend my career wearing the 49ers jersey was special to me," he said via conference call from his home in Tennessee. "And this team is heading in the right direction -- I wanted to be a part of it."
"We are really happy to come to an agreement on a contract extension with Dan before free agency," 49ers general manager John Lynch said in a statement. "He is very much a leader of this team, and his hard work and commitment to our success provide a great example for our young team. When you come across players who love the game like Dan you do your best to keep them in your building."
For the first time since becoming a starter, Kilgore was able to play in all 16 games in 2017 after winning a short-lived competition for the job against Jeremy Zuttah in training camp.
Still, Kilgore admitted wondering whether the 49ers would consider other options on the free-agent market.
"It kind of makes you nervous because in this profession, people like the younger guys and you just never know what will happen at any time, any given day in the NFL," he said. "So yeah, toward the end, that last day I was cleaning out the locker and I didn't know if I would be back."
Kilgore, 30, was a 2011 fifth-round pick by the Niners. He emerged as a locker room leader and one of the team's most dependable veterans over the past couple of seasons.
He also developed a solid relationship with quarterback Jimmy Garoppolo over the season's final two months and repeatedly said he hoped to stay in San Francisco.
The 49ers re-signed Garoppolo to a five-year, $137.5 million extension last week.
"I think there's a special relationship between the center and quarterback," Garoppolo said after last season. "We spend so much time together. Me and him got on the same page almost instantly when I got here. He made my job a lot easier in the run game and all that stuff. We complement each other very well, so hopefully we'll work something out."
In seven seasons with the Niners, Kilgore has started 39 games, all of which came since 2014.
ESPN's Nick Wagoner contributed to this report.