Juventus and Real Madrid may have won their groups but both suffered shock defeats on Wednesday, while Ajax and Bayern Munich contested a six-goal thriller as they battled for top spot in Group E.

Juve needed a win against Young Boys to make sure of top spot in Group H, but slumped to their first competitive away defeat in more than a year as Guillaume Hoarau scored twice to earn the Swiss side’s first ever group stage win.

Champions League last-16 draw: who’s hot, who’s not and who can play who Read more

Cristiano Ronaldo hit the post and missed several first-half chances, which were punished when Hoarau put the hosts ahead from the penalty spot after Alex Sandro had hauled down Nicolas Moumi Ngameleu. The Cameroon international set up Hoarau’s second after 68 minutes, with the French forward cutting inside and drilling into the bottom corner.

Paulo Dybala pulled a goal back for Juventus, and thought he had rescued a point in stoppage time – but his goal was disallowed after Ronaldo was adjudged to be in an offside position. Despite the defeat, Max Allegri’s side still topped the group after Manchester United failed to get the win they needed, going down 2-1 at Valencia.

Earlier, a much-changed Real Madrid team suffered their biggest ever home defeat in European football, losing 3-0 to CSKA Moscow. Goals from Feder Chalov and Georgi Schchennikov gave the Russian side an unexpected 2-0 lead at half-time.

Facebook Twitter Pinterest CSKA players mob Arnor Sigurdsson after he scored their third goal at the Bernabéu. Photograph: Shot for Press/Action Plus via Getty Images

Real had already secured top spot in Group F but could ill afford another defeat after their poor start to the season. Gareth Bale came on for the second half but could not change the hosts’ fortunes as Iceland international Arnor Sigurdsson struck a third in the 73rd minute.

“Although we were through, we wanted to finish the group with a win, and we didn’t do that,” the Real Madrid coach Santiago Solari said afterwards. “We started the game well, but we weren’t good enough in either box and they punished us. I didn’t like what I saw in the second half.”

CSKA, despite their historic win, were denied third spot in the group as Viktoria Plzen defeated Roma 2-1, meaning the Czechs progressed because of a better head-to-head record. Tomas Chory scored the winner with a diving header after 72 minutes.

Ajax and Bayern Munich played out a thrilling 3-3 draw in Amsterdam, with the hosts needing a win to leapfrog their opponents at the top of the group. Bayern took an early lead through Robert Lewandowski, before Dusan Tadic’s equaliser brought the game to life with an hour played.

Facebook Twitter Pinterest Dusan Tadic celebrates with Frenkie de Jong after scoring from the penalty spot. Photograph: Emmanuel Dunand/AFP/Getty Images

Ajax defender Maximilian Wober was shown a straight red card for a wild lunge,with Bayern’s Thomas Müller following him down the tunnel seven minutes later. Tadic then scored from the spot to put Ajax ahead with less than 10 minutes to play, only for Lewandowski to level in the 87th minute after Bayern won a penalty of their own.

Kingsley Coman then struck from distance as the game went into injury time, but Ajax found an equaliser as Nicolas Tagliafico tapped home Klaas-Jan Huntelaar’s cross. Bayern held on to win Group E, but Ajax will be dangerous opponents in the last 16. In the group’s other game, a late goal from Benfica’s Alex Grimaldo ensured AEK Athens ended their campaign without a point.

Lyon are into the knockout stages of the Champions League for the first time since 2012 after Nabil Fekir’s equaliser in Kiev sent them through at the expense of Shakhtar Donetsk. The hosts led at half-time through Junior Moraes but Fekir’s curling second-half effort sent the French side through with a win and five draws from their six Group F matches.