In the last eight she will face Wimbledon champion Marketa Vondrousova, who overcame a rocky start to beat another American Peyton Stearns 6-7 (3-7) 6-3 6-2.
Keys crashed out of the first round at Cincinnati last month but appeared right at home on the New York hard courts, where she was runner-up in 2017, as she overwhelmed her opponent with 21 winners.
Pegula beat the 17th seed Keys in their only previous meeting last year and was considered as one of the brightest home hopes after winning the Montreal title last month.
However, on Monday she lacked her usual firepower and finesse.
"I've had so many amazing moments in New York," said Keys, who is playing in her 12th US Open main draw.
"Being able to at any moment come back from any difficult positions I've been in matches has been amazing."
Keys, who counts Pegula as a close friend, pumped her fist in a subdued celebration as the top-ranked American dropped her serve again when she sent a forehand shot into the net on match point.
"It's always tough having to play a friend... when we get on the court it's all business," she said in courtside interview.
Ninth seed Vondrousova fell a set behind to Stearns but the American youngster won just five more games.
"I actually didn't expect it after Wimby, there was a lot of pressure," Vondrousova said in her postmatch interview. "Let's see what happens next."
There are few clues as to what might happen next for the 24-year-old Czech as she and Keys have never played each other before.