NEW YORK — Sloane Stephens was two points from defeat against Venus Williams in one US Open semifinal before pulling out the victory.
Madison Keys faced no such test, overwhelming CoCo Vandeweghe in the other semifinal.
Now Stephens and Keys, a pair of pals in their early 20s, will meet in the first Grand Slam title match for each — and the first all-American women’s final at Flushing Meadows since 2002.
Stephens summoned some of her best strokes when she needed them the most, steeling herself when so close to defeat and taking the last three games of a back-and-forth thriller, edging seven-time major champion Williams, 6-1, 0-6, 7-5, on Thursday night.
‘‘I have a lot of grit,’’ said Stephens, who is ranked 83rd after having surgery on her left foot in January and is the fourth unseeded finalist at the tournament in the Open era, which dates to 1968. ‘‘I don’t give up. Like, I'm not just going to give it to someone. I'm not just going to let them take it from me.’’
The 15th-seeded Keys, who dominated No. 20 Vandewegh, 6-1, 6-2, had her own recent health issue to deal with: She missed the first two months of this year after an offseason operation on her left wrist, then needed another procedure in June because of pain in that arm.
‘‘It was kind of one of those days where I came out and I was kind of in a zone,’’ Keys said of her play Thursday, ‘‘and I just kind of forced myself to stay there.’’
She had 25 winners to only nine unforced errors, never faced a break point and needed barely more than an hour to win.
‘‘Madison played an unbelievable match,’’ said Vandeweghe, who wiped away tears during her news conference. ‘‘I didn’t really have much to do with anything out there.’’
It would have been even shorter, except play was delayed for more than five minutes when Keys left the court to have her upper right leg taped at 4-1 in the second set. She said she first felt something in that leg in her previous match but made it sound as if the treatment was simply a precaution.
‘‘I just didn’t want it to become something that would be bad,’’ Keys said. ‘‘So as soon as I kind of felt it get the tiniest bit worse, I just had it wrapped to try and prevent anything from happening.’’
This was the first time in 36 years that all four women’s semifinalists at the US Open represented the host country, so it was understandable if spectators in Arthur Ashe Stadium were conflicted about which players to pull for.
At 37, Williams was attempting to become the oldest woman to win a Grand Slam title in the Open era. She was trying to reach her third major final of this season, something she last did 15 years ago. Here’s how long and successful her career has been: Williams’ first final in New York came in her US Open debut in 1997. Stephens, now 24, was 4 at the time.
‘‘Venus knows it’s an opportunity lost,’’ said her coach, David Witt, ‘‘because she had it. She had it on her racket.’’
Williams was ahead 5-4 with Stephens serving at 30-all. Two points away. That’s when they engaged in a 25-stroke point, until Stephens conjured up a backhand passing winner down the line, then wheeled and pumped her fists.
‘‘That was good, huh?’’ Stephens said later.
At 5-all, Stephens broke with the help of a rainbow of a winner — ‘‘That lob-thingy,’’ she'd call it — that drew a standing ovation from the crowd, and a full-sprint get of a short ball that she turned into a ‘‘How did she do that?!’’ point-ender at an impossible angle.
‘‘There was nothing I could do about those shots,’’ Williams said.
Soon enough, Stephens was serving out the biggest win of her career — and of this comeback. She returned to the tour at Wimbledon in July, losing in the first round, and lost her next match, too. Her ranking, which reached a high of No. 11 in 2013, dropped out of the top 900.
But since then, Stephens has won 14 of 16 matches.
‘‘Sloane is a new person right now,’’ the 22-year-old Keys said. ‘‘I think she’s really loving being back on the court again and she’s just excited to be out here and playing really well.’’
Williams vs. Stephens was a back-and-forth affair, with a pair of lopsided sets leading up to a classic third. Just when it seemed one woman or the other was in full command, the match would swerve in a new direction.
They both hit the ball hard. They both covered so much ground, Williams getting to seemingly unreachable balls thanks to her long wingspan, Stephens doing the same thanks to her speed.
But it was Williams who faltered. She finished with six double-faults and a total of 51 unforced errors, 24 more than Stephens.
‘‘I just wasn’t playing well. Those are moments where you have to dig deep and figure out how to get the ball on the court and have a big game. I can’t be tentative and try to figure out how to put that ball in,’’ Williams said. ‘‘Clearly she’s seen me play many, many times. I haven’t seen her play as much.’’
Stephens and Keys have only played once before, on a hard court at Miami in 2015. Stephens won in straight sets.
Away from the court, though, they know each other well. On Saturday, one will raise a Grand Slam trophy for the first time, at the other’s expense.
‘‘She’s one of my closest friends on tour. It’s obviously going to be tough,’’ Stephens said. ‘‘It’s not easy playing a friend.’’