The Red Devils needed a victory after being shock in their opening group game against Young Boys.
But United were staring at another defeat when Paco Alcacer gave the away side the lead after 53 minutes.
United battled back and restored parity through Alex Telles' stunner on the hour-mark.
The English giants pushed for a winner and Ole Gunnar Solskjaer sent on Jesse Lingard in the top he would get them the goal they craved.
And the 28-year-old repaid Solskjaer's faith by playing a huge role in United's winner in the 95th minute.
Lingard laid the ball off in the box to Cristiano Ronaldo, who converted from a tight angle to send the home supporters into raptures.
The Portuguese forward will get all the plaudits, but it wouldn't have been possible without Lingard's contribution.
Close-up footage shows just how good his assist was.
Lingard showed great reactions to nip in front of two Villarreal defenders for the ball.
He could have had an effort at goal but he spotted Ronaldo unmarked and in space.
The Englishman delivered a perfect touch and then had the presence of mind to duck so he didn't get in the way of Ronaldo's shot.
Lingard deserves as much credit as Ronaldo for the goal.
It was a brilliant cameo from Lingard, who had a bit of a nightmare in United's opening loss to Young Boys.
Solskjaer sent the Englishman on late against the Swiss outfit in the hope he could give them the victory.
Ronaldo's last-minute winner! Manchester United 2-1 Villarreal | The Football Terrace
But it was his errant back-pass that actually led to Young Boys' winner. He made up for that error and more on Wednesday evening.
United are now up to third in Group F. They could go top if they defeat Atalanta in Italy in their next game, which takes place on October 20.