Robbie Benson lit up an uneventful first half with a stunning sweetly-struck right-foot volley into the top corner from the edge of the area.
The hosts had Adam Hlousek sent-off for a second yellow card on 67 minutes.
Michal Kucharczyk equalised with a composed solo effort in added time.
Dundalk will go into the Europa League group stages, emulating the achievement of fellow League of Ireland side Shamrock Rovers in 2011. The draw will be made on Friday.
Legia Warsaw will await the outcome of the Champions League draw in Monaco on Thursday after becoming the first club from Poland to reach the group stages for 20 years.
The visitors, who secured a league and cup double in Ireland last season, had qualified for the qualifying play-off by defeating Belarusian champions Bate Borisov 3-1 in the previous round, aided by a memorable 3-0 second-leg victory.
Dundalk boasts a population of just 39,000 and the club went into the second leg of their tie at the Polish Army Stadium having already banked more than £4m for their European exploits, with more set to come as their European adventure continues.
Dundalk's chances of progression appeared to be slim after their 2-0 defeat at the hands of the Polish champions in Dublin last week, but Benson's magnificent first-half strike was just reward for Stephen Kenny's side's efforts in the opening half.
Benson had come into the side to replace suspended captain Stephen O'Donnell and his perfectly executed goal was his fourth of the season and his second in the competition.
Benson went close to grabbing a second in the 63rd minute but his overhead bicycle kick, after David McMillan headed into his path, flew over the bar.
Hlousek received his second booking four minutes later for hauling down Ronan Finn but Dundalk were unable to take advantage of the extra man.
Kucharczyk's thunderous late strike on the break extinguished the Irish side's hopes and exploited the gaps in their defence as they pushed forward in search of the goal that would have taken the game to extra-time.
Match ends, Legia Warsaw 1, Dundalk 1.
Second Half ends, Legia Warsaw 1, Dundalk 1.
Lukasz Broz (Legia Warsaw) wins a free kick in the defensive half.
Foul by Daryl Horgan (Dundalk).
Foul by Guilherme (Legia Warsaw).
Dane Massey (Dundalk) wins a free kick on the left wing.
Substitution, Legia Warsaw. Aleksandar Prijovic replaces Thibault Moulin.
Michal Kucharczyk (Legia Warsaw) is shown the yellow card for excessive celebration.
Goal!  Legia Warsaw 1, Dundalk 1. Michal Kucharczyk (Legia Warsaw) right footed shot from the centre of the box to the bottom right corner. Assisted by Vadis Odjidja-Ofoe following a fast break.
Michal Kucharczyk (Legia Warsaw) wins a free kick in the defensive half.
Foul by Patrick Barrett (Dundalk).
Offside, Legia Warsaw. Guilherme tries a through ball, but Thibault Moulin is caught offside.
Attempt saved. Guilherme (Legia Warsaw) left footed shot from the centre of the box is saved in the top right corner. Assisted by Michal Kucharczyk.
Substitution, Dundalk. Michael O'Connor replaces Patrick McEleney.
Delay over. They are ready to continue.
Delay in match Guilherme (Legia Warsaw) because of an injury.
Offside, Legia Warsaw. Vadis Odjidja-Ofoe tries a through ball, but Michal Kucharczyk is caught offside.
Substitution, Dundalk. Darren Meenan replaces Chris Shields.
Attempt blocked. Michal Kucharczyk (Legia Warsaw) right footed shot from outside the box is blocked. Assisted by Bartosz Bereszynski.
Attempt blocked. Andrew Boyle (Dundalk) header from the centre of the box is blocked. Assisted by Daryl Horgan with a cross.
Corner,  Dundalk. Conceded by Igor Lewczuk.
Foul by Thibault Moulin (Legia Warsaw).
Sean Gannon (Dundalk) wins a free kick in the attacking half.
Attempt saved. Patrick McEleney (Dundalk) right footed shot from the right side of the box is saved in the centre of the goal. Assisted by Daryl Horgan.
Corner,  Legia Warsaw. Conceded by Sean Gannon.
Attempt blocked. Bartosz Bereszynski (Legia Warsaw) right footed shot from the left side of the box is blocked. Assisted by Thibault Moulin.
Robbie Benson (Dundalk) is shown the yellow card for a bad foul.
Michal Kucharczyk (Legia Warsaw) wins a free kick on the right wing.
Foul by Robbie Benson (Dundalk).
Substitution, Legia Warsaw. Bartosz Bereszynski replaces Nemanja Nikolic.
Guilherme (Legia Warsaw) wins a free kick on the right wing.
Foul by Chris Shields (Dundalk).
Second yellow card to Adam Hlousek (Legia Warsaw) for a bad foul.
Foul by Adam Hlousek (Legia Warsaw).
Ronan Finn (Dundalk) wins a free kick in the defensive half.
Substitution, Legia Warsaw. Guilherme replaces Steeven Langil.
Foul by Igor Lewczuk (Legia Warsaw).
David McMillan (Dundalk) wins a free kick on the left wing.
Attempt missed. Robbie Benson (Dundalk) left footed shot from the centre of the box is too high. Assisted by David McMillan with a headed pass.
Corner,  Dundalk. Conceded by Adam Hlousek.
Dundalk's attempt to become the first Irish side to make the Champions League group stages fell just short as they lost 3-1 to Legia Warsaw on aggregate after a creditable 1-1 draw in Poland.