CSKA had to beat Plzen at a snowy Luzhniki Stadium on Tuesday to have any chance of finishing in Group G's top two spots, but their loss ensured reigning champions Madrid and Roma advanced.
The home side took an early lead from the penalty spot, Nikola Vlasic converting to put Viktor Goncharenko's men on track for the win they needed.
But after Igor Akinfeev's penalty save from Roman Prochazka, the Plzen midfielder made amends with an equaliser and Hejda headed home with nine minutes left to complete a fine comeback.
CSKA opened the scoring in the 10th minute, Vlasic on target from the penalty spot for his third Champions League goal of the season.
Milan Petrzela tripped Georgy Schennikov in the box and Vlasic, on loan at CSKA from Everton, beat Ales Hruska easily from 12 yards.
CSKA continued to create chances with Schennikov heading over a fine right-wing Mario Fernandes cross, then badly missing from close range under pressure from goalkeeper Hruska.
Having won CSKA's spot-kick, Schennikov gave one away in similar circumstances by fouling Radim Reznik, but Akinfeev dived low to his left to keep out Prochazka's penalty.
Prochazka made amends with an excellent equaliser in the 56th minute, picking out the top-right corner after substitute Jan Kopic's cross found him in the box.
Ivan Oblyakov had a great chance to put CSKA back in front but failed to connect with Fedor Chalov's pass, then Hruska made a fine stop to keep out a powerful Arnor Sigurdsson shot.
But with CSKA committing men forward in search of the winner they needed to stay in the competition, they were caught out at the other end as Plzen gained revenge for a 2-2 draw earlier in the group stage.
Plzen, who led that previous meeting 2-0 before collapsing, worked a short corner to Patrik Hrosovsky and he delivered a fine cross for defender Hejda to nod home.
What does it mean? Europa League battle beckons
CSKA created enough chances in the first half to win several matches but they could not put them away and lost the opportunity to stay in the competition as a result. They will now compete with Plzen for third place to keep their European adventure going into 2019.
Prochazka bounces back
The Champions League will not see many worse penalties this season than Prochazka's effort, but the Plzen midfielder showed strong character to equalise for his side, keeping their own European hopes alive. His finish, produced despite slipping, was a moment of quality.
Schennikov switches off
CSKA's back three meant wing-back Schennikov had licence to get forward but he struggled to balance his defensive duties with attacking forays. Schennikov conceded a penalty due to a lapse in concentration and his two first-half misses proved extremely costly.
Key Opta Facts
- Viktoria Plzen picked up just their second ever Champions League away win (W2 D0 L7) – their other one was back in 2011-12.
- This was just the second time in the Champions League that Plzen won a game in which they conceded the first goal – the other was also 2-1 against CSKA Moscow, back in December 2013.
- Vlasic has been directly involved in four of CSKA Moscow’s five Champions League goals this season (3 goals, 1 assist).
- Akinfeev has faced seven penalties in the Champions League, with this one being the first he has saved.
Viktoria Plzen - occupying third place - host Roma on matchday six in a fortnight, while Group G's new bottom side CSKA have Real Madrid away.