Financial terms of the deal weren't disclosed.
Gooden, who's 6-foot-1 and 242 pounds, played in 26 games with 12 starts in three seasons with the Baltimore Ravens, recording 66 tackles on defense and 22 tackles on special teams.
Niners defensive coordinator Vic Fangio, who was Gooden's position coach in Baltimore from 2008 to 2009, said Gooden is a special-teams upgrade who will be a backup linebacker in San Francisco.
"Well he's a good athlete; he can run very well," Fangio said. "He's been a good special-teams player throughout his career, and I think he just improves our overall speed. Again, he has an ability to cover kicks and block on kick returns."
Gooden was a third-round draft pick by the Ravens in 2008 out of the University of Miami. He played in 10 games last season.
The Associated Press contributed to this report.