Conor Masterson and Todd Kane scored their first goals for QPR as they won an eventful derby.
There were also goals for Ebere Eze and the outstanding Ryan Manning in Rangers’ final home match of the season.
After Masterson put the hosts ahead, Matt Smith scored against his former club to haul Millwall level four minutes into the second half.
Rangers responded with goals from Manning and Eze before Shaun Hutchinson reduced the deficit.
Kane made it 4-2 with a 72nd-minute diving header – his first touch after coming on as a substitute.
And there was late drama after Jayson Molumby’s shot deflected in off Yoann Barbet deep into injury time.
Millwall thought they had equalised when Hutchinson headed in with seconds remaining, but the offside flag was raised and for the second time the visitors had a goal disallowed.
Centre-back Masterson netted a couple of minutes before half-time, firing home from close range after Bartosz Białkowski had parried Barbet’s header from Eze’s right-wing corner.
Two crosses from the left by Shane Ferguson had almost led to Millwall going ahead.
His first delivery was misjudged by keeper Joe Lumley, and Jon Dadi Bödvarsson – on as a substitute for the injured Mason Bennett – managed to make contact but headed against the post.
Lumley atoned for that error by diving to his right to push away Tom Bradshaw’s header from Ferguson’s second cross.
Białkowski was also called into action early on, producing two saves in quick succession.
After Białkowski kept out Ilias Chair’s fierce left-footed strike, Olamide Shodipo retrieved the ball and laid it back to Luke Amos, whose effort was also saved by the Millwall keeper.
Białkowski did too well to keep out Barbet’s firm header, but Masterson was on hand to put the R’s ahead.
The Pole produced another fine save shortly after the interval, diving to his left to push away another strike from Chair.
However, Białkowski was at fault for Manning’s goal shortly after Jed Wallace had done well on the right and pulled the ball back for Smith to thump in the equaliser.
Manning’s low left-footed shot from near the edge of the penalty area bounced just in front of Białkowski and into the far corner of the net.
He then hit the bar from a similar position and set up a chance for Chair, who shot wide.
Manning played a key part in QPR’s third goal, scored just after the hour mark.
Manning dispossessed Connor Mahoney and played the ball forward to Chair, whose lay-off enabled Eze to slot home his 13th goal of the season.
Millwall thought they had pulled a goal back courtesy of Alex Pearce’s follow-up after Lumley had saved Smith’s header, but the defender was offside.
Hutchinson did pull one back with a header from Wallace’s 67-minute corner, but Kane’s goal restored Rangers’ two-goal advantage – and again Manning was involved.
Dominic Ball collected Manning’s ball down the left-hand side and delivered a cross into the box, where right-back Kane arrived at the far post to score.
Subs not used: Kelly, Clarke, Gubbins.