LEE Gregory scored a 98th-minute penalty for Millwall to seal a 2-1 victory over Gillingham at Priestfield.
Aiden O'Brien gave the Lions the lead 10 minutes after the break before Mahlon Romeo was shown a straight red after 86 minutes. John Egan equalised for the Gills in the 92nd minute before Gregory's dramatic winner.
The result meant Millwall leapfrogged Bradford to finish fourth and they will be at home to the Bantams in the second leg of the play-off semi-final.
Neil Harris named an unchanged starting XI ahead of the final League One game of the regular season.
Gregory returned to the squad after an operation on a persistent abdominal injury, with Shaun Williams also coming back into the travelling party after serving a three-match ban for his dismissal at Coventry.
The initial exchanges of the fixture were like a pre-season match with the opening 10 minutes a slow, sluggish affair.
The home side were looking to the channels with former Gills full-backs Joe Martin and Mahlon Romeo dealing well with balls played in behind them.
The hosts had the first sight of goal when Deji Oshilaja headed over from Doug Loft’s corner, before the same player sent a second header over the bar from a second Gillingham corner.
Minutes later, the Lions were awarded a free-kick just outside the area when Chris Taylor was brought down by Egan.
Martin stepped up to take it but his effort was narrowly wide of the left-hand post after taking a deflection off a team-mate.
The game then sprung into life when, after 20 minutes, Steve Morison played in O’Brien who advanced on goal, but the angle was tight and he fired a low effort straight at goalkeeper Stuart Nelson.
Shane Ferguson's corner then eventually found its way to Mark Beevers. The defender's shot was saved by Nelson and parried into the path of Martin whose deflected effort crashed against the woodwork.
Gillingham had their first attempt on target when Bradley Dack’s corner found the head of Oshilaja for a third time, but Jordan Archer saved well low at his feet.
The second half started in the same manner as the first with both sides slow to get out of the traps.
Gillingham again looked to get down the channels, but again to no avail.
Then, after 55 minutes, the deadlock was broken.
Ben Thompson did well to find space in midfield and played a slick ball through to Taylor. The midfielder advanced before passing to Morison whose shot was parried and fell invitingly into the path of O’Brien and he rifled home on the stretch to send the traveling 1,213 Millwall fans into a frenzy.
From then on, Millwall looked comfortable and restricted Gillingham to a few chances.
Archer did save well from Oshilaja after Dack’s header had fallen into his path.
Millwall almost doubled their lead with 13 minutes remaining when Taylor’s corner was met by Morison’s head but Nelson got his fingertips to the ball to knock it against the woodwork for a second time.
Romeo was shown a straight red card for an apparent over-the-top tackle and, barring an appeal, the right-back will be ruled out of the play-off campaign.
There were six minutes of added time shown by the fourth official, and within two minutes of that Gillingham equalised, Egan smashing home off the crossbar from a tight angle.
With time running out and Millwall seemingly set to finish fifth in the table, Gregory, who had come on for O’Brien with 20 minutes left, was fouled in the box.
Referee Paul Tierney, after seeing his linesman signalling, pointed to the spot in the eight minuted of injury time to give the Lions the chance to secure a fourth win in a row, and fourth spot in the division.
And Gregory stepped up under enormous pressure to send his spot-kick into the roof of the net to give the Lions a last-gasp victory and secure a home second leg in the play-off semi-final.
While the Lions fans were still celebrating the latest of goals, the referee blew up for full-time to end the regular League One season.