As Michael Phelps powered home the anchor leg of the U.S. 4x200-meter freestyle relay, he did something he had never done before underwater during a race: He started smiling with about 20 meters left. He could not help himself.

When he touched the wall in 6 minutes 59.7 seconds, more than three seconds ahead of second-place France, Phelps had sealed not merely his first gold medal of these Olympics, he had seized history. In claiming his 19th Olympic medal in his fourth Games, Phelps surpassed the record held by Soviet gymnast Larisa Latynina.

When he got out of the water, he grabbed his three teammates, wrapped his arms around them and offered a history lesson they surely didn’t need.

“I want you guys to know,” Phelps said, “I just became the most decorated athlete ever. I want to thank you guys.

“We didn’t have much to say,” said Ricky Berens, recalling the moment later. “We’re usually thanking him.”

Phelps won a record eight golds at the 2008 Summer Games in Beijing, and six golds and two bronze medals in 2004 in Athens. At these Games so far, he has earned two silvers and a gold, with hopes of winning three more. After 48 years, Phelps finally broke the mark held by Latynina, who won her medals (nine gold) between the 1956 and 1964 Olympics.

“There are a lot of emotions going through my head right now,” Phelps said. “I’m going to attempt to sleep tonight. I’m not sure that’s going to be possible.”

The historic evening did not start according to plan. After AmericanAllison Schmittwon the gold in the women’s 200 freestyle, Phelps planned to make it two in a row in a race he had not lost in a world championship or Olympic Games since 2001: the 200-meter butterfly.

But he finished second.

Worse, he got passed over the last 10 meters by South African Chad le Clos, who lunged to the wall first to edge Phelps by .05 seconds and then could not contain his tears of joy.

Phelps had tied the career medals record with his finish in 1:53.01, yet found himself furious. The 200 fly had always been a special event to him. His sisters competed in the event. He made his first Olympic team in 2000 in the 200 fly, finishing fifth. When he became the youngest man to set a world record at 15 years 9 months old in 2001, it was in the 200 fly.

He held a nine-year, 60-victory streak in the event until China’s Wu Peng beat him in April 2011. This, he had always said, was a Phelps family event. So instead of celebrating the silver medal, he ripped off his swim cap and threw it in the water.

“The last one I would have liked to win,” Phelps said later. “Sure I was upset. You can tell I wasn’t happy . . . [But] the biggest thing with the relays is you have a team. I wanted to do everything I could not to let them down.”

And so in the ready room before the relay later, Phelps approached his teammates and broke his customary silence. Usually he listens intently to the hip-hop music flowing through his headphones. Tuesday, he had something to say.

“We do this together,” he announced, raising his fist to punctuate the thought. “This is for us.”

He has been doing this throughout these Games, U.S. swimmers have said, giving speeches, handing out encouragement, surprising them with his demeanor.

“Michael has taken on a really unique leadership role this year,” said Kathleen Hersey, who competed in the 200 fly semifinals. “He’s the greatest athlete of all time in our sport. Whenever he says anything, it means more just because of him saying it.”

Phelps said he had another message for his teammates, one more selfish: Give him a big lead. Knowing he had just competed in a grueling final less than an hour before, they accommodated the request. The U.S. gold never seemed in jeopardy from Ryan Lochte’s opening leg in 1:45.15, which put Team USA 0.58 seconds ahead of the second-place Australians. For Lochte, who had finished fourth in the 200 final the night before and lost a late lead in the 4x100 relay Sunday, the race offered a chance to regain some self-respect.

“The past two days, I wasn’t myself,” Lochte said. “I think after that relay, my confidence went down. Everybody just kept telling me, ‘You’re better than that. Forget it and move on.’ ”

Conor Dwyer and Berens stayed strong, completing their legs in 1:45.23 and 1:45.27, handing Phelps a lead of nearly four seconds. Even Yannick Agnel, the 200 champion and France’s anchor, could not close the gap. Phelps came home in 1:44.05. The French finished in 7:02.77, with China getting the bronze in 7:06.30.

When Phelps touched the wall, he hung on a lane line, grinning, as his mother and sisters screamed with joy and cried, and U.S. flags bobbed throughout the arena. He took a mouthful of water and spewed it skyward.

After the race, he tried to reach his mother in the stands, pushing a plastic chair toward the elevated front row and standing on it with his arm outstretched as she reached down.

They didn’t connect, at least physically. It didn’t matter. By then, Phelps couldn’t stop grinning.

“This still is fun for me,” Phelps said. “I love being here.”