KASHIMA, Japan (AP) — Megan Rapinoe and Carli Lloyd each scored a pair of goals and the United States won the bronze medal in women’s soccer at the Tokyo Olympics with a 4-3 victory over Australia on Thursday.

It was arguably the best the Americans had looked during the course of a rocky tournament that opened with an uncharacteristic 3-0 loss to Sweden. Rapinoe set the tone early with a goal scored directly from a corner kick.

The loss spoiled the Australians’ first-ever trip to the medal round at the Olympics. No Australian soccer team, men or women, has ever won a medal.

The Matildas were the underdogs against the United States, the top-ranked team in the world and the defending World Cup champions who came to Japan hoping for a fifth gold medal. But the Americans struggled at times during the tournament, including a 1-0 loss to Canada in the semifinals. The Canadians hadn’t defeated their North American counterparts since 2001.

The U.S. team that came out against Australia looked completely different.

Rapinoe scored from a corner kick in the eighth minute to give the Americans an early lead. It was the second time Rapinoe has scored an Olimpico, as goals from corners are known: She also had one in the semifinals of the 2012 Olympics against Canada.

Sam Kerr pulled the Australians even with a goal in the 17th minute that got past U.S. goalkeeper Adrianna Franch.

Franch got her first start in a major tournament in place of Alyssa Naeher, who injured her right knee in the U.S. team’s semifinal loss to Canada.

Just 27, Kerr became the all-time leading scorer for the Australians with 48 goals. She led all scorers remaining in the tournament with six goals.

Rapinoe’s second goal came on a stunning volley in the 21st, and it became obvious that the Australians were missing defender Ellie Carpenter, who was handed a red card late in the Matildas’ 1-0 semifinal loss to Sweden.

Lloyd made it 3-1 in first-half stoppage time when her left-footed shot got past Australia goalkeeper Teagan Micah. She added another on a break in the 51st minute, giving her a U.S. record 10 career Olympic goals.

Lloyd made her 312th appearance for the national team in the match, passing Christie Rampone for second on the all-time list. Kristine Lilly leads the career appearances list with 354.

Lloyd is 39 and likely playing in her last major tournament. When she was replaced in the 81st minute, teammates ran over to offer handshakes and high-fives, and the bench gave her a standing ovation.

Caitlin Foord kept the Australians in the game with a goal in the 54th minute before Emily Gielnik scored to narrow it further in the 90th, but ultimately the Matildas couldn’t make up the difference.

Alex Morgan got hurt in stoppage time and had to be helped off the field. It appeared that she injured her left leg but there was no immediate word on her condition.

The Australians had already met the United States at the Tokyo Olympics, holding the Americans to a scoreless draw in the final game of the group stage. Australia was also the only team in the group to score on the Swedes, who are headed for the gold medal match Friday against Canada.

The U.S. team’s tournament-opening loss to to Sweden snapped a 44-match unbeaten streak. After a rebound victory over New Zealand came the scoreless draw with Australia. In the quarterfinals, the U.S. got past the Netherlands on a penalty shootout.

But they were thwarted in a quest to become the first team to win an Olympics after a World Cup title by their Canadian rivals. Jessie Fleming scored on a penalty kick in the 74th minute for the game’s lone goal.

The United States has reached the gold medal match at the Olympics five times. The Americans have four gold medals, more than any other team since women’s soccer became an Olympic sport in 1996.

But it is the second Olympics they have failed to reach the gold medal match. They were beaten by Sweden in the quarterfinals five years ago in Brazil.