The team announced the deal Friday. Brooks had received a second-round tender but was able to reach a longer-term deal to stay in San Francisco.
Brooks set a career-high last season with six sacks and tied the franchise record for forced fumbles by a linebacker with four.
Brooks entered the league as a third-round pick by Cincinnati in the 2006 supplemental draft. He started seven games in two seasons with the Bengals before being released in 2008.