EVERETT, WA, USA -- It went down to the wire, but the United States moved into the Fed Cup by BNP Paribas Finals on Saturday night, surviving an incredible comeback by Latvia to notch a 3-2 victory in their Fed Cup Qualifier tie.

In the end, Sofia Kenin and Bethanie Mattek-Sands were too strong on the doubles court, defeating Jelena Ostapenko and Anastasija Sevastova, 6-4, 6-0 in the fifth and decisive rubber to make the U.S.A. the twelfth and last team to clinch a spot in the 12-team Fed Cup by BNP Paribas Finals in Hungary in April.

Read more: Sabalenka, Sasnovich send Belarus to Fed Cup final, Slovakia through

However, it had been a heroic effort by Ostapenko and Sevastova to even get to that point. The Latvian duo picked up sterling Top 10 singles wins to pull level after an 0-2 start on Friday, and give themselves a chance to become just the eighth team to win a Fed Cup tie from 0-2 down since the best-of-five-matches format started in 1995.

2017 Roland Garros champion Ostapenko started the day with a 6-3, 2-6, 6-2 win over newly-crowned Australian Open champion and World No.7 Sofia Kenin.

Sevastova then caused a massive shock when she beat 23-time Grand Slam champion Serena Williams, 7-6(5), 3-6, 7-6(4). It was the first time Williams had ever lost a Fed Cup singles match, ending her undefeated record in the discipline at 14-0.

Nevertheless, the American team still prevailed over the weekend due to the brilliant doubles play of Kenin and former WTA Doubles World No.1 Mattek-Sands at the end of a long day for the two squads.

In the doubles clash, Ostapenko saved multiple break points in each of her first two service games, but that was not enough to prevent an early deficit for the Latvians, when Mattek-Sands found a backhand slice winner down the line to break Sevastova and give the Americans an early advantage at 2-1.

That would be the only break the Americans would need in the opening frame, as they eased to the one-set lead without facing a break point.

A deft lob winner by Kenin gave the Americans double break point in the first game of the second set, and they converted the chance after Ostapenko found the net with a backhand on the second opportunity.

It was smooth sailing for Kenin and Mattek-Sands from there, as they cruised to victory after an hour and a quarter without ceding another game, propelling the United States to the final berth at the Fed Cup Finals in two months.

Ostapenko had been the first to put Latvia in contention for the comeback victory when she ended the eight-match winning streak of Kenin after just under two hours of engrossing play. Signs that the meeting might go Ostapenko’s way were evident throughout the first set, when hard-hitting tennis by the Latvian culminated with back-to-back forehand winners for the one-set lead.

However, Kenin used her steely mentality and incredible defense to force Ostapenko into errors and build an early 4-0 lead in the second set. Ostapenko was able to pull one of the breaks back, but faltered with a love service break at 5-2 to give Kenin the second set, as well as hope that the new World No.7 could engineer a stirring comeback.

But the power game of Ostapenko came back in spades in the decider. The Latvian’s forehand helped her fend off two break points and hold for 3-1, as well as force an error to earn another break of the Kenin serve and a 4-1 advantage. The Latvian kept dominating from that side -- 21 forehand winners on the day -- to close out the match.

The fourth rubber, between Williams and Sevastova, opened with five straight breaks of service, but it would be Sevastova who stopped that run and held for 4-2. Sevastova broke Williams to lead 5-2 before the Williams power game clicked into place, as the American pulled to 5-5.

Williams seemed to be eking ahead, holding three set points at 6-5, but Sevastova erased them all and queued up a first-set tiebreak. In the tightly contested breaker, it was Sevastova who got to set point first, with a winning dropshot to lead 6-5. There, Williams misfired wide on a backhand down the line, giving Sevastova the one-set lead.

However, Williams took command of the second set with precise power, rolling to a 4-1 lead after Sevastova double-faulted two straight service games away. Even a late break by the Latvian at 5-2 could not derail Williams, who broke straight back, at love with a scorching backhand return winner, to close out the set and level the affair.

In an eerie contrast to the opener, there were no break points in the decider, as the duo moved inexorably to a decisive tiebreak. At 4-4, Williams ended the next two points with forehand miscues, giving Sevastova double match point. On the first, a strong serve by the Latvian was returned short, giving Sevastova her first victory over the American legend.

The U.S. team, though, would be undeterred in the doubles to clinch the tie, thwarting the stunning fightback from the Latvian twosome.

In other Qualifier ties, two-time Fed Cup champions Germany polished off an undefeated weekend against home team Brazil to claim a spot in the Fed Cup by BNP Paribas Finals. Laura Siegemund clinched the tie with a 6-1, 6-2 victory over Gabriela Ce. Siegemund broke Ce seven times while fending off six of the eight break points she faced.

Four-time Fed Cup champions Russia also moved into the Finals, but they needed to win the decisive doubles match against home team Romania to do so. Deadlocked at 2-2, Anna Blinkova and Anna Kalinskaya took out Jaqueline Cristian and Elena-Gabriela Ruse, 6-3, 6-2, ending the run of the 2019 semifinalists.

2001 Fed Cup champions Belgium notched their spot in the Finals with a pair of straight-set singles wins over Kazakhstan on Saturday. Elise Mertens took out Yulia Putintseva, 6-1, 7-6(1), and Kirsten Flipkens defeated Zarina Diyas, 6-3, 6-4.

Finally, Switzerland prevailed over Canada to book their flight to Hungary in April. 17-year-old Leylah Fernandez shocked World No.5 Belinda Bencic, 6-2, 7-6(3), in the third rubber, for the teenager’s first Top 5 win. But Jil Teichmann sent the Swiss through in the end, after beating Gabriela Dabrowski, 6-3, 6-4.