SAN DIEGO — Julie Ertz came in off the bench and scored in the 89th minute and the United States overcame a late two-goal deficit to beat Brazil 4-3 on Sunday night in the Tournament of Nations.

The United States was in danger of losing for the fourth time this year before the three-goal flurry in about nine minutes.

Two minutes after Brazil’s Andressinha scored on a free kick in the 78th minute for her second goal of the game, Christen Press countered to make it 3-2 with her 43rd career goal and second of the year.

Press fed Megan Rapinoe for a spectacular running blast that tied it in the 85th minute, and Ertz put the Americans ahead four minutes later. Ertz is using her married name after playing as Julie Johnston.

Rapinoe, who was recovering from knee surgery for much of last year, had not scored for the national team since the 2015 World Cup. She has rebounded this season with the NWSL’s Seattle Reign and leads the league with 12 goals.

The United States was coming off a 1-0 loss to Australia in the team’s Tournament of Nations opener in Seattle on Thursday night. It was the first time that the Australians beat the Americans 28 previous matches.

Coach Jill Ellis tinkered with her lineup against Brazil, surprisingly moving Becky Sauerbrunn to a defensive midfielder role rather than her usual spot at center back.

The United States has never dropped four games on home soil in a single season. In addition to Australia on Thursday, the Americans fell to England and France in the She Believes Cup in March.

The Americans have won just four matches when they’ve allowed an opponent three goals.

In Sunday’s early game at Qualcomm Stadium, Sam Kerr scored three goals for her first international hat trick and the Australian women’s national team beat Japan 4-2.

Kerr scored in the 10th, 14th and 43rd minutes, capping the trio of goals with a backflip.

Australia, ranked No. 7 in the world, leads the tournament standings. The Matildas will wrap up the round-robin tournament against Brazil on Thursday in Carson, California. The United States will face Japan.