The Turkish side were second best until a 72nd-minute Dossa Junior own goal gave them a fortuitous lead.
Olcan Adin, whose cross led to Dossa Junior's mistake, then sealed victory seven minutes later following a counter-attack after Legia chased their first point of the campaign.
Trabzonspor now need just a single point from their final two games to progress to the last 32.
Legia had defied their lowly position in the Group J standings by having the best of the opening half.
Michal Kucharczyk made an early nuisance of himself as he forced Trabzonspor goalkeeper Onur Kivrak to tip a 12-yard effort wide. Kivrak was again unable to hold on when Kucharczyk tried his luck from outside the area.
Tomasz Jodlowiec volleyed wide from the edge of the area as Legia's confidence grew, although a mis-directed 30-yard shot from Dominik Furman was ambitious.
Trabzonspor offered little in the opening 45 minutes with a well-hit 35-yard effort from Jose Bosingwa about the best they could muster.
Legia continued to threaten after the break, roared on by a vocal home support, with Dossa Junior heading just wide after getting in front of his marker to meet Furman's delivery to the back post.
Trabzonspor were dealt a blow when Kivrak had to be replaced with what appeared a leg injury on 53 minutes. His replacement Zeki Ayvaz was immediately called into action to tip over Tomasz Brzyski's heavily deflected strike.
But despite Legia's dominance they fell behind to a goal against the run of play with 18 minutes left.
Dossa Junior was unlucky to concede it when Florent Malouda made a hash of trying to head in an Adin cross. The former Chelsea winger managed only to get a slight touch off his shoulder but Dossa Junior had too little time to react as the ball hit him and went in.
Malouda then turned provider for Adin moments later as Trabzonspor hit Legia on the break.
The momentum of the move had looked lost when Malouda had a shot blocked, but after retrieving the ball he tricked his way into the area and pulled a pass back for Adin to slide in a low shot.