Italy completed a superb fightback to knock Japan out of the Confederations Cup and book their place in the semi-finals in a hugely entertaining game.
Japan went 2-0 up via Keisuke's Honda's penalty and Shinji Kagawa's strike.
But Italy responded in some style when Daniele De Rossi headed in before Atsuto Uchida's own goal and Mario Balotelli's penalty made it 3-2.
Japan equalised through Shinji Okazaki and also hit the woodwork, before Sebastian Giovinco sealed it for Italy.
Alberto Zaccheroni's Japan side could be justified in feeling the result in Recife was harsh on them.
Having surrendered a two-goal lead, then recovered from going behind to bring the scores level again after 69 minutes, they saw Okazaki and Kagawa hit post and bar respectively with eight minutes left.
So when Giovinco - a first-half substitute for Alberto Aquilani in an opening period dominated by Asia's champions - popped up to tuck in Claudio Marchisio's cross four minutes from time it was a crushing blow for the team that had showed the greater enterprise over 90 minutes.
The result ends Japan's involvement here but the Brazilian crowd lapped up their quick passing and movement and will welcome them back to next year's World Cup, for which they have already qualified.
Ultimately Cesare Prandelli's earned the win through a ruthlessness Japan could not match, and they join Brazil in the last four as qualifiers from Group A. But they struggled to begin with as Manchester United's Kagawa - a potential star of next year's global showpiece - created countless problems for the normally resolute Italian backline.
Japan looked a completely different side to the one that lost to Brazil in their opening match, their passing fluid and their tempo higher than the Italians.
The breakthrough was a gift, however, as Mattia De Sciglio left his backpass short and Gianluigi Buffon was adjudged, perhaps harshly, to have brought down Okazaki. The keeper was shown a yellow card and Honda scored the spot-kick.
With Italy being outplayed, Giovinco's early introduction was an attempt to give more balance to Prandelli's side. But Kagawa gave Japan the more emphatic lead they deserved when he fired in a shot after the Italian defence failed to deal with Yasuyuki Konno's looping ball into the box.
De Rossi was then booked - he will miss the final group game against Brazil - as the Italians looked in danger of falling apart. But out of nowhere, the Roma midfielder led a surprising Italian turnaround before the break.
First, he headed in Andrea Pirlo's corner after 41 minutes, before Emanuele Giaccherini hit the post in first-half stoppage time. And five minutes after the interval Italy got the equaliser they sought when Uchida knocked the ball into his own net after Giaccherini's persistence.
The Azzurri's revival continued when Argentine referee Diego Abal awarded a second contentious spot-kick, penalising Japan captain Makoto Hasebe for a handball despite the ball bouncing off the floor to hit him from Giaccherini's shot.
Balotelli produced his customary stuttering run-up then stroked Italy into a scarcely credible lead.
But in sweltering conditions, the Italians seemed to fade again and paid the price when Okazaki nodded home Yasuhito Endo's free-kick as the game swung once more.
Japan were inches away from winning it as Buffon's goal led a charmed life, but Giovinco added the final twist as Italy rejoiced.
Even then Japan almost equalised again but Maya Yoshida's effort was ruled out for offside after Okazaki's shot had come back off the bar.
Italy coach Cesare Prandelli: "Tonight we really, really struggled. We need to deal with the climate, it's really difficult. But I liked the way we suffered. We could have felt satisfied with ourselves but instead we wanted the win."