Italy scored three times in 11 minutes either side of half-time, through Daniele De Rossi, Atsuto Uchida's own goal and Mario Balotelli's penalty after Japan had established a 2-0 lead courtesy of Keisuke Honda's spot-kick and Shinji Kagawa's neat finish.
Shinji Okazaki made it 3-3 in the 69th minute, but Sebastian Giovinco netted four minutes from the end to give Italy the points and ensure they will progress from Group A alongside hosts Brazil.
Japan's early dominance was rewarded in the 21st minute when, from a short Mattia De Sciglio backpass, Gianluigi Buffon stormed off his line to meet Okazaki, sliding into ball and player.
The referee hedged his bets by giving a penalty but not sending Buffon off, and Honda dispatched an unconvincing spot-kick past the goalkeeper.
It was 2-0 just after the half-hour mark as Kagawa took advantage of some uncertainty in the Italy defence, spinning and firing low past Buffon at the near post.
Having been outplayed for 40 minutes, Italy got themselves back in the game from a corner four minutes before the break. Andrea Pirlo's outswinging delivery was perfect and De Rossi got ahead of his marker to thump a header past the keeper.
Italy almost levelled in first-half stoppage-time as Emanuele Giaccherini shot sharply on the turn, sending the ball against the base of the post. But they did not have to wait long after the restart to get back on level terms as Japan handed them an equaliser.
Maya Yoshida attempted to shield the ball out of play, but Giaccherini stole it off him, centred the ball and a sliding Uchida poked the ball into his own net.
Moments later Italy were in the lead thanks to another controversial penalty.
Makoto Hasebe was harshly adjudged to have handled as he made a sliding block, and Balotelli converted from the spot.
Japan could have been forgiven for letting their heads drop but they were level again in the 69th minute when Okazaki made a great run to the near post to meet Yasuhito Endo's free-kick and headed home.
With nine minutes to play, Japan went agonisingly close to a fourth as Okazaki's shot from the edge of the box hit the post and the rebound went quickly to Kagawa, whose header bounced off the turf, on to the crossbar and away. And they were made to pay as, against the run of play, substitute Giovinco turned home Claudio Marchisio's cross with four minutes remaining.
Still Japan almost equalised as Okazaki hit the bar and Yoshida converted the rebound, only to be denied by the offside flag.