LOS ANGELES, CA — Anthony Ervin of Valencia won a gold medal for his swim in a heat of the men's 4x100 meter freestyle relay in the 2016 Olympics on Sunday in Rio de Janeiro.

With the 35-year-old Ervin swimming the anchor leg in 47.65 seconds, the fastest of the team's four legs, the U.S. won its heat in three minutes, 12.38 seconds. Ryan Held was the only U.S. swimmer to swim in both the heat and final. In the final, Michael Phelps swam the second leg and turned a 0.13-second deficit into a 1.04-second lead. The Americans kept the lead for the final two legs to give Phelps his 23rd Olympic medal and 19th gold, extending both his records.

The gold medal is the second for Ervin. He tied teammate Gary Hall Jr. for first in the 50 freestyle in the 2000 Olympics, with both winning gold medals. Ervin won a silver medal in 2000 that year in the 4x100 meter freestyle relay. He was fifth in the 50 freestyle in the London Olympics. The team of Phelps, Caeleb Dressel, Held and Nathan Adrian finished with a time of 3:09.92, 0.61 seconds ahead of France, which won the silver in 3:10.53. Australia won the bronze medal in 3:11.37.

James Feigen and Blake Pieroni also won gold medals for swimming in the heat. Ervin assured himself of a spot on the 4x100 meter freestyle relay team with a fourth-place finish in the 100 freestyle in the U.S. Olympic Trials June 30. Ervin became the oldest American to qualify for an individual Olympic swimming event since 1904 with a second-place finish in the men's 50 freestyle at the Olympic Trials on July 2.

Heats and semifinals in the men's 50 freestyle will be held Thursday, with the final set for Friday.

Ervin was among six swimmers selected by team members as a captain.

Ervin graduated from Hart High School in Santa Clarita in 1999, then attended University of California, Berkeley. Ervin said he began swimming when he was less than 1 year old and had an "early fascination with the feel and the freedom."