Sunderland chairman Ellis Short said Moyes was “by far and away first choice” to take control of the Black Cats and that he had tried to recruit him several times before.
“He is a man whose football pedigree speaks for itself and is someone I have long admired,” said Short.
“I have spoken with him on many occasions and I have always been hugely impressed.
“He was my number one managerial target for the last five appointments, but his desire to honour existing contracts meant we were not able to bring him to Sunderland previously. To be able to finally welcome him as our manager is fantastic news for the club.”
Managerial awards – but tough times at Old Trafford
Moyes, who started his managerial career at Preston North End, was voted LMA Manager of the Year three times during an 11-year spell at Everton.
He succeeded Sir Alex Ferguson as Manchester United boss on his fellow Scot’s recommendation when he retired after a trophy-laden 26 years in charge at Old Trafford.
But despite signing a six-year deal with the then Premier League champions, he was sacked 10 months later and went on to manage Real Sociedad in Spain.
“I look forward to continuing the good work done by Sam,” said Moyes of the Sunderland job.
He will be the club’s fourth manager in less than three years, following Gus Poyet, Dick Advocaat and Allardyce.
Former Bolton, Newcastle and West Ham manager Allardyce joined Sunderland in October 2015, with the club second bottom in the Premier League and winless in their opening eight games.
But they recovered to finish 17th, one place above their relegated north-east rivals Newcastle United.