By Mark Trevelyan

RIO DE JANEIRO (Reuters) - Michael Phelps and Ryan Lochte set up one final showdown to cap their glittering Olympic careers by qualifying first and second on Wednesday for the final of the 200 meters individual medley.

Racing in adjacent lanes in the second semi-final, Phelps clocked one minute, 55.78 seconds and Lochte 1:56.28, a turnaround from the morning heats where Lochte was first and Phelps third.

Brazil's Thiago Pereira, who had led the Americans after the butterfly and backstroke legs and was second behind Phelps at the final turn, qualified third in 1:57.11 to give the home country a shot at a first medal in the pool.

Japan's Kosuke Hagino, winner of the 400 IM on Saturday, won a slower first semi-final to go through to Thursday's final in fourth place.

In the same event at London in 2012, Phelps took the gold medal and Lochte won silver.

Phelps, 31, is bidding to become the first swimmer to win the same event at four consecutive Olympics, and to expand a career medal haul that has grown this week to 25, all but four of them gold.

Lochte, a year older, has 12 Olympic medals, six of them gold, and the 200 IM is his only individual event in Rio after taking gold alongside Phelps in Tuesday's 4x200 freestyle relay.

(Additional reporting and writing by Mark Trevelyan; Editing by Toby Davis/Greg Stutchbury)