Michu had cancelled out Javier Hernandez's first-half strike, and the game, almost incidental to Sunday's double farewell celebrations, appeared to be heading towards a 1-1 draw before Ferdinand popped up to score his first goal in five years in the 87th minute.
The fact that Manchester United scored a late winner seemed quite fitting in Ferguson's last appearance in the Old Trafford dugout in a Red Devils career that has spanned nearly 27 years.
The result came after Ferguson was given a guard of honour by both sides before kick-off in an emotionally-charged final appearance for the Scot.
He will be replaced next season by David Moyes, who also enjoyed victory in his final home game as Everton manager.
Striker Kevin Mirallas ensured Moyes departed Goodison Park in perfect circumstances with a 2-0 win over West Ham.
The victory also guaranteed Everton finished above Liverpool for the second successive season.
In the relegation fight, Norwich confirmed their top-flight survival with an emphatic 4-0 victory over West Brom at Carrow Road.
Robert Snodgrass gave the Canaries a half-time lead before a Ben Foster howler gifted Grant Holt the second and former Ipswich defender Gareth McAuley's own goal prompted Delia Smith to dance with delight.
The Baggies were woeful and Jonny Howson scored a spectacular late fourth as Chris Hughton's men claimed a third win in 20 Premier League games to make certain of their top-flight status ahead of next week's final-day trip to Manchester City.
In contrast, Sunderland will be anxiously watching Wigan's match at Arsenal on Tuesday as they were held to a 1-1 draw by Southampton.
Jason Puncheon came off the bench to grab a point for Southampton and all but secure their safety, just as it looked as though Sunderland had saved themselves in their clash at the Stadium of Light.
The midfielder struck with 14 minutes remaining after fellow substitute James Ward-Prowse provided the ammunition to cancel out Phil Bardsley's opener, and in the process left the home side still looking over their shoulders.
The result leaves Sunderland on 39 points with one game to play - against Champions League-hunting Tottenham - while Wigan, who are four points adrift of the Black Cats, also have a home match against Aston Villa on the final day of the season.
Newcastle ensured their Premier League survival with a game to spare as they beat already-relegated QPR 2-1 at Loftus Road on Sunday.
Hatem Ben Arfa's penalty and a Yoan Gouffran strike helped the Magpies come from behind to secure a victory that ensured another season in the top flight after Loic Remy's early spot-kick had put Harry Redknapp's side ahead.
Newcastle goalkeeper Rob Elliot's late sending off did not affect the result, with the Magpies holding out for a vital three points.
Tottenham moved back above north London rivals Arsenal and into a Champions League qualifying position as they saw off 10-man Stoke with a 2-1 win at the Britannia Stadium.
Emmanuel Adebayor slid home a late winner to lift Tottenham back into fourth place after they fell behind to a Steven Nzonzi header after just three minutes, before Clint Dempsey equalised with a brilliant piece of improvisation.
Stoke midfielder Charlie Adam was dismissed in the 47th minute for a second bookable offence and Spurs were made to labour before their late winner.
Elsewhere, Daniel Sturridge fired Liverpool to an easy 3-1 win at Fulham with a superb hat-trick at Craven Cottage.
Sturridge had not scored against the Cottagers during his time at the club's local rivals Chelsea or while he was at Manchester City, but he took Martin Jol's team apart on Sunday with three well-taken strikes.
Three minutes after Dimitar Berbatov had nodded Fulham ahead, the England striker turned Aaron Hughes inside-out before smashing the ball into the roof of the net.
On Saturday, Frank Lampard became Chelsea's leading goal-scorer with a brace as the Blues all but secured a Champions League spot for next season with a 2-1 win over Aston Villa who had goal-scorer Christian Benteke sent off for two bookable offences.
Sunday's Premier League Results
Saturday's Premier League Result