Phelps started well on the butterfly leg, his strongest stroke, but Lochte took command when they switched to the breaststroke. He built a lead of about a half-body length and held off Phelps in the freestyle finish, cruising to the wall with one arm extended to post a time of 4 minutes, 7.06 seconds. He got a kiss from his father, Steve, as he came off the deck.
Phelps claimed the second Olympic spot in 4:07.89.
"I was very pleased with that," Phelps said. "I said if I went 4:07, I'd be happy."
Tyler Clary, who took second in the 2011 worlds, won't even get a chance to swim the event in London. He faded to third in 4:09.92 and was so upset he didn't even bother to stop for the media in the mixed zone.
Heading to his third Olympics, Lochte is determined to repeat his brilliant performance last year in Shanghai, where he surpassed Phelps as the world's top swimmer.
"I'm definitely ready tear it up in London and show the world what the USA is all about," Lochte said.
Don't count out Phelps, who plans to retire after the Olympics and is eager to end his career with another dazzling performance. He already has won 14 gold medals, more than any other athlete.
That he is even swimming the 400 IM shows Phelps has regained the focus and dedication that faded away after the 2008 Olympics. In those Games, Phelps won his second consecutive gold medal in the event but insisted he was done with the grueling race. Then, in the last few months, he put it back in his repertoire - and now he will be swimming it again in London.
That means he could take another shot at eight gold medals, the mark he set in 2008 to eclipse Mark Spitz's Olympic record.
Phelps will be heavily favored to claim a spot on the U.S. team in four other individual events in Omaha: the 100 and 200 butterfly, 200 freestyle, and 200 individual medley. If he again swims on all three American relay teams, as expected, Phelps will have another eight-event program in the Olympics.
In other finals on the opening night of the trials, Peter Vanderkaay became a three-time Olympian by winning the 400 freestyle, while 19-year-old Elizabeth Beisel earned her second straight trip to the Olympics in the 400 individual medley.
Vanderkaay won with a time of 3:47.67, while Conor Dwyer settled for the second spot on the Olympic team in 3:47.83. Beisel won her event easily in 4:31.74, more than two seconds ahead of Caitlin Leverenz.
Dana Vollmer, bouncing back from the disappointment of failing to qualify for the Olympic team four years ago, set an American record in the semifinals of the 100 butterfly. The defending world champion put up a blistering time of 56.42 seconds to edge the mark she set a year ago (56.47).
The swimmers got a rude awakening when a hotel fire alarm sounded on the first day of the trials.
Phelps was in his room napping after the morning preliminaries.
"Well I have always had a prob with waking up from naps. . . . But not possible to sleep through that fire alarm . . ." he tweeted.
Asked about it after his final, Phelps said, "I was pretty upset and walking down eight flights of steps."
Lochte said he was jolted awake by the alarm.
"That scared me," he said before joking: "I heard some little kid on the floor pulled it. I was like, 'Oh, I'm going to find that kid.' "