Briton Mo Farah joined the ranks of the all-time distance greats on Saturday by becoming just the second man to retain both Olympic track long distance titles after adding a brilliant 5,000m gold to his 10,000 at the Rio Games.

On a sparkling final night of athletics, Matthew Centrowitz won a shock 1,500m gold for the United States, their first in the event since 1908.

The nation also won both 4x400m relays to stretch their winning margin in the track and field medal count.

Caster Semenya cruised to victory in the women's 800m, Germany's Thomas Roehler won the javelin and 37-year-old high jumper Ruth Beitia won Spain's first Olympic gold in women's athletics.

The Rio athletics program closes with the men's marathon on Sunday.

With Usain Bolt finally moving aside after his triple-triple heroics had dominated the first nine days of action, Farah took center stage for his double-double to emulate Finland's Lasse Viren (1972 and 1976).

Having also done the double twice at the world championships, Farah proved he is up there with the best endurance performers of all time.

He never looked like being beaten on Saturday, winning gold with a consummate display of control and finishing power.

"To do it again is incredible," said Farah. "I have that drive, it's just me.

"I can't quite believe it. I wished for just one medal as a junior. It has been a long journey but if you dream of something, have ambitions and are willing to work hard then you can get your dreams."

Centrowitz might have been dreaming of a minor medal but found himself in the box seat with a brilliant tactical run in the 1,500 and had too much finishing speed for defending champion Taoufik Makhloufi of Algeria.

"Doing my victory lap, I literally kept screaming to everyone I know, 'Are you kidding me?'" Centrowitz said.

There was no shock in the women's 800m where Semenya delivered gold, the first Olympic athletics title for a South African woman in 64 years, having looked unbeatable all year and having to deal with the pressure of questions over her gender.

"The coaches told me to just focus on running, nothing else," said Semenya, who is reported to produce excess testosterone that many feel gives her an unfair advantage.

"Sport is meant to unite people. I think that's what we need to keep doing."

In the women's 4x400m relay, the United States collected their sixth straight gold in a streak going back to Atlanta 1996, with Allyson Felix bumping her career gold medal tally up to six.

In the final event of the night, perhaps fittingly given the sport's recent travails, former doper LaShawn Merritt ran a blistering anchor leg to return the men's 4x400m title to the United States.

After being shocked by the Bahamas four years ago, they turned the tables to record their 17th victory in the event.

That took their athletics gold medal tally to 13, with Jamaica second on six and Kenya on five.