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.
Maguire 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."