The 10th-seeded Nishikori won 4-6, 7-6 (4), 6-7 (6), 7-5, 6-4 in a match that ended at 2:26 a.m. Tuesday, tying the latest finish in tournament history.
Raonic couldn't get anything going against Nishikori's serve in the final two sets, unable to force a single break point. And Nishikori earned the two breaks he would need.
"Tried to fight every point, and when I have to play well, I did," he said.
Nishikori pushed Raonic all over the court, finishing off the match with an easy volley after 4 hours, 19 minutes. He will next face Australian Open champ Stan Wawrinka.
"His foot speed was the most probably difficult part," Raonic said. "He was taking the ball very early, controlling the center of the court. He was keeping himself in a lot of situations where someone might be out of position. He was getting himself in good position and giving himself good opportunity to swing at the ball properly."
Raonic couldn't follow up his historic semifinal appearance at Wimbledon with another deep Grand Slam run. He got in only 55 percent of his first serves, and Nishikori took advantage.
A severely infected right big toe forced Nishikori to miss tuneup events before the U.S. Open, but the layoff didn't seem to hamper his stamina. Nishikori, who got his foot re-wrapped early in the fourth set, looked like the fitter player at the end.
"I was doing a lot of training, but not tennis-wise. I start playing points a couple days ago before the tournament," Nishikori said. "I wasn't expecting big result like this, but after the first round I get more confidence on my foot. It's all good now."
With the crowd at Arthur Ashe Stadium thinning deep into the night, Japanese fans chanted loudly for Nishikori. The 24-year-old had lost to the 23-year-old Raonic in this round at Wimbledon in a matchup of two of the tour's top young players. Raonic went on to become the first Canadian man in the Open era to reach a Grand Slam semifinal.
Philipp Kohlschreiber's third-round win over John Isner in the 2012 U.S. Open, and Mats Wilander's second-round victory against Mikael Pernfors in 1993 also ended at 2:26 a.m.
Earlier Monday evening, trailing against a woman trying to become the first qualifier in the U.S. Open quarterfinals since 1981, Victoria Azarenka needed every ounce of experience and skill to advance.
Twice a major champion, and twice a runner-up at Flushing Meadows, Azarenka took four of the last five games Monday night to come back and win 4-6, 6-4, 6-4, ending the surprising, out-of-nowhere run by 145th-ranked Aleksandra Krunic of Serbia.
"Obviously, I didn't have a lot of data on her," the 16th-seeded Azarenka said about her 5-foot-4 opponent. "I was a little bit surprised that she's not that tall, and she hits the ball and unleashes her forehand with so much power."
The varied game played by Krunic, a 21-year-old from Serbia, worked wonders earlier in the tournament, when she used her mix of spins and speeds to eliminate two-time Wimbledon champion Petra Kvitova in the third round, and 27th-seeded Madison Keys in the second.
That run of success, which followed three victories in qualifying rounds, meant that Krunic, as she described it, "started maybe loving myself a little bit more."
"Everybody can hit forehand or backhand or serve," Krunic said. "Yeah, it's more about how you get it all together."
By making it to the fourth round, she earned a check for $187,300 -- nearly matching her career take coming in. Asked if she knew what her U.S. Open prize money would be, Krunic replied: "No. I know it's 30 percent tax. That makes me very sad."
Her ranking would have been the lowest for a quarterfinalist in U.S. Open history, although other women -- such as 2009 champion Kim Clijsters -- have made it that far without any ranking at all because of a lack of activity.
And the crowd showered Krunic with loud support throughout the 2-hour, 19-minute match under the lights in Arthur Ashe Stadium, something she said surprised her.
Azarenka, who lost to Serena Williams in the 2012 and 2013 U.S. Open finals, took a more aggressive approach in the latter stages of the match, pushing forward to win nine of 10 points at the net in the final set.
Azarenka was down 3-2 in that set, but broke to go ahead 4-3 with a backhand winner down the line off one of Krunic's many attempted drop shots.
When the match ended, and Azarenka was pulled aside for an on-court interview, Krunic stuck around so the players could walk off together -- the sort of thing that rarely happens at the U.S. Open.
Azarenka has won the Australian Open twice and used to be ranked No. 1, but she has struggled during an uneven 2014 because of health problems.
She entered Monday having played 20 matches all season, including one from January to June because of a left foot injury, and only four in the time between Wimbledon and the U.S. Open because of a bothersome right knee.
She bristled at her news conference when a reporter used the word "suffered" while referencing Azarenka's problems.
"You're making it sound like ... I almost died and, you know, there was 10 sharks, and I got attacked, and I survived. And, you know, I saved a dolphin, as well," Azarenka said. "It's not that complicated, really. What I enjoy is to play tennis. To be talking about what happened with my injuries, I mean, it's useless already."