The deal is worth $54 million with $27.5 million guaranteed, a source told ESPN's Adam Schefter.
The Ravens decided not to use the franchise tag on Williams, which would have paid him $13,387,000 in 2017.
Williams, who turned 28 in February, went from being a third-round pick from Division II Missouri Southern State to one of the most coveted free agents available in 2017.
Williams' strength and athleticism on the interior has made him one of the league's best run-stoppers. Baltimore has given up the third-fewest rushing yards (93.8) since he became a starter in 2014.
His 48 tackles ranked as the third most by a nose tackle last season. For his career, Williams has totaled 4.5 sacks, three batted-down passes, three forced fumbles and one blocked field goal.
ESPN's Jamison Hensley contributed to this report.