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 just 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 over Mikael Pernfors in 1993 also ended at 2:26 a.m.