Teenager Alejandro Garnacho scored an outstanding goal as Manchester United ended Real Sociedad's flawless Europa League record, but had to settle for second place in Group E.
Erik ten Hag's side needed to win by two goals or more in Spain to finish top of the group, and their spirits were lifted when Garnacho showed terrific pace before firing Cristiano Ronaldo's pass into the corner of the net.
It was a wonderful moment for the 18-year-old Madrid-born winger, who was making his second start for the Red Devils.
Ronaldo missed a great chance to double the lead with a lob on to the roof of the net, before David de Gea produced a fine double save to keep out Andoni Gorosabel and Pablo Marin.
United finish the group stage level on 15 points with Sociedad, who advance to the last 16 with a superior goal difference.
The Red Devils will now play in February's knockout-round play-offs, where they will face one of the clubs dropping down from the Champions League.
Teams they could meet over two legs include Barcelona, Ronaldo's former club Juventus, Ajax, who Ten Hag managed for five years, and Ukraine's Shakhtar Donetsk.
Bayer Leverkusen, Sporting Lisbon, Red Bull Salzburg and Sevilla are also in Monday's draw for the knockout round.
There are 19 years and 147 days between the ages of goalscorer Alejandro Garnacho (18 years and 125 days) and Cristiano Ronaldo (37 years and 271 days), who provided the assistMaguire finishes up front
Had United scored one more goal without conceding, they would have advanced as group winners and avoided two extra games in February.
They were in complete control in the first half but did not manage a single shot on target after half-time, and ended the match with substitute Harry Maguire up front alongside Ronaldo and Marcus Rashford.
On a wet night in the Basque Country, Garnacho became United's youngest non-English scorer in Europe at the age of 18 years and 125 days.
Four weeks ago the Argentina youth international scored a late winner for United's under-21 side in the EFL Trophy at Barrow, and last Thursday made his full debut in the 3-0 Europa League win over Sheriff Tiraspol at Old Trafford.
In San Sebastian, Garnacho showed what a talent he is with a composed 17th-minute finish after receiving Ronaldo's well-timed pass outside the penalty area before finishing inside the box.
Ten Hag praised Garnacho's "great finish" a week after revealing he had been irked by the teenager's attitude earlier in the season - a situation Bruno Fernandes expanded on in Spain.
"He is still only young but he is doing very well," said Fernandes after the game.
"At the beginning of the season he was not at his best. He didn't have the best attitude that he should have on tour. But he is deserving of his chances now."
Garnacho's goal was the perfect start for Ten Hag's side and the youngster was presented with another opportunity but slipped and fired over the bar.
Real Sociedad were missing several players because of injury, including four-time Premier League winner with Manchester City David Silva.
Yet they just about did enough to finish top of the group in a tetchy game that saw a flurry of yellow cards in the second half.
"We win today but of course we are disappointed we didn't score two goals because that is what we needed," added Ten Hag, whose side are unbeaten in their past nine domestic and European games.
"We have to wait for the draw. We have two extra fixtures and it's a hard season but we have a squad, we have to deal with that and we will do."
Player of the match
GarnachoAlejandro Garnacho
Real Sociedad
Squad number5Player nameZubeldia
Squad number17Player nameNavarro
Squad number15Player nameRico
Squad number34Player nameMagunacelaya
Squad number24Player nameLe Normand
Squad number42Player nameMarín
Squad number6Player nameElustondo
Squad number3Player nameZubimendi
Squad number20Player namePacheco
Squad number23Player nameMéndez
Squad number16Player nameGuevara
Squad number8Player nameMerino
Squad number19Player nameSørloth
Squad number18Player nameGorosabel
Squad number9Player nameFernández
Squad number1Player nameÁlex Remiro
Line-ups
Real Sociedad
Formation 4-3-1-2
1Remiro Gargallo
18Gorosabel24Le Normand20Pacheco15Rico
23Méndez3Zubimendi8Merino
42Marín
19Sørloth9Fernández
1Remiro Gargallo18GorosabelSubstituted forElustondoat 58'minutes24Le Normand20Pacheco15Rico23MéndezBooked at 64minsSubstituted forZubeldiaat 83'minutes3Zubimendi8Merino42MarínSubstituted forNavarroat 59'minutes19SørlothSubstituted forMagunacelayaat 83'minutes9FernándezBooked at 73minsSubstituted forGuevaraat 73'minutesSubstitutes
2Sola4Illarramendi5Zubeldia6Elustondo13Zubiaurre16Guevara17Navarro22Turrientes30González de Zárate32Marrero33Karrikaburu34MagunacelayaMan Utd
Formation 4-2-3-1
1de Gea
20Dalot2Lindelöf6Martínez23Shaw
18Casemiro14Eriksen
8Bruno Fernandes34van de Beek49Garnacho
7Cristiano Ronaldo
1de Gea20DalotBooked at 87mins2LindelöfSubstituted forMcTominayat 58'minutes6MartínezBooked at 62mins23Shaw18Casemiro14EriksenSubstituted forFredat 82'minutes8Bruno Fernandes34van de BeekSubstituted forRashfordat 58'minutes49GarnachoSubstituted forMaguireat 82'minutes7Cristiano RonaldoBooked at 71minsSubstitutes
5Maguire10Rashford12Malacia17Fred28Pellistri29Wan-Bissaka31Dúbravka36Elanga39McTominay47Shoretire50Vítek55IqbalLive Text
Match ends, Real Sociedad 0, Manchester United 1.
Second Half ends, Real Sociedad 0, Manchester United 1.
Corner, Manchester United. Conceded by Mikel Merino.
Offside, Manchester United. Cristiano Ronaldo tries a through ball, but Casemiro is caught offside.
Diogo Dalot (Manchester United) is shown the yellow card for a bad foul.
Foul by Diogo Dalot (Manchester United).
Jon Magunacelaya (Real Sociedad) wins a free kick in the attacking half.
Corner, Real Sociedad. Conceded by Diogo Dalot.
Foul by Lisandro Martínez (Manchester United).
Jon Magunacelaya (Real Sociedad) wins a free kick in the defensive half.
Foul by Harry Maguire (Manchester United).
Jon Pacheco (Real Sociedad) wins a free kick in the defensive half.
Substitution, Real Sociedad. Jon Magunacelaya replaces Alexander Sørloth.
Substitution, Real Sociedad. Igor Zubeldia replaces Brais Méndez.
Substitution, Manchester United. Fred replaces Christian Eriksen.
Substitution, Manchester United. Harry Maguire replaces Alejandro Garnacho.
Attempt missed. Alexander Sørloth (Real Sociedad) left footed shot from the left side of the box misses to the right. Assisted by Brais Méndez.
Attempt blocked. Robert Navarro (Real Sociedad) left footed shot from the right side of the box is blocked.
Attempt missed. Alexander Sørloth (Real Sociedad) left footed shot from outside the box is high and wide to the left following a fast break.
Diego Rico (Real Sociedad) wins a free kick in the defensive half.