Carlo Ancelotti's side were stunned when Japan took the lead with a superb strike from Kaishu Sano after 29 minutes, but replied when Casemiro atoned for a poor first-half performance with a fine header.
The game appeared to be heading towards extra time after a tense final period but Martinelli, sent on by Ancelotti on 66 minutes, capped the fightback after being played in by Bruno Guimaraes.
It was harsh on an obstinate Japan side, who were chasing their country's first World Cup knockout win, but the five-time world champions finished strongly after a lethargic first-half display.
"We can never be content with what we're doing," Ancelotti said through a translator.
"We're doing a good job. We are performing. But you can never be content because we want to play better. We want to play at the highest level."
The South Americans found it difficult to create openings but did have an early chance as a Bruno Guimaraes shot was deflected behind and Matheus Cunha had an effort pushed wide by Zion Suzuki.
Sano, booked early on after catching Vinicius Junior late, escaped further punishment for a clumsy challenge on Cunha.
That was to prove controversial as the Mainz midfielder gave Asia's top nation the lead just before the half-hour, seizeing on Danilo's loose pass and powering past Casemiro before burying a low shot beyond Alisson Becker.
Brazil were reduced to tame long-range efforts from Vinicius and Cunha in response, but Ancelotti's halftime adjustments prompted a change of tempo.
Guimaraes tested Suzuki with a strong header and Casemiro had an even better chance moments later, planting his header against Takehiro Tomiyasu on the line when he seemed certain to score.
Casemiro made no mistake when he got another chance on 56 minutes, heading past Suzuki at the back post from a Gabriel cross.
Vinicius almost put Brazil ahead when he weaved his way through and attempted to flick over Suzuki, but the keeper managed to touch on to the post.
Five minutes of added time had elapsed when Guimaraes slipped in Martinelli and he bent a shot past Suzuki.
"The gap between us is closing now," Japan coach Hajime Moriyasu said through a translator.
"Brazil is a top-tier team and we're definitely approaching that level.
"To win the title was our dream and our goal and we couldn't achieve that ... I told them that I was really sorry that I was not good enough to lead them to that level to achieve that goal."
Brazil will face either Ivory Coast or Norway in New Jersey in the last 16.
With the AP.