The deal has an average annual value of US$2.6 million ($2.8 million in 2019-20, $2.4 million in 2020-21).
Acquired in a deal with the Vancouver Canucks in January, Nilsson, 29, emerged as the Senators’ backup goalie behind Craig Anderson.
The six-foot-six native of Sweden had an 11-11-0 record with a 2.90 goals-against average, 914 save percentage and two shutouts with the Senators.
Nilsson has a career NHL record of 50-65-13 with a 3.07 GAA, .907 save percentage and six shutouts in 141 games with the New York Islanders, Edmonton Oilers, St. Louis Blues, Buffalo Sabres, Canucks and Senators.
Nilsson was a third-round pick of the Islanders (62nd overall) in the 2009 NHL draft.
"We’re very happy to have Anders back for two more seasons," Senators general manager Pierre Dorion said in a statement.
"He is a competitive, talented veteran who played very well after being acquired last season. In addition, Anders is a great teammate and hard worker, which makes him an excellent fit with our growing team. This signing provides us with stability at the goaltending position, which is very important to our success as our team continues to develop."
The 29-year-old was set to become an unrestricted free agent on July 1.