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 Brazil finished strongly after a lethargic first-half display.
The South Americans found it difficult to create openings but did have an early chance as a 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 Sano gave Japan the lead just before the half-hour.
The Mainz midfielder seized on Danilo's loose pass and powered 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 half-time adjustments prompted a change of tempo.
Guimaraes tested Suzuki with a strong header and Casemiro had an even better chance moments later but planted 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.
Japan were reeling and Vinicius almost put Brazil ahead when he weaved his way through the area and attempted to flick over Suzuki, but the keeper just managed to touch on to the post.
Japan recovered their composure and held Brazil back for long periods but the South Americans eventually found a way through.
Five minutes of stoppage time had elapsed when Guimaraes slipped in Martinelli and he bent a shot past Suzuki.
Brazil will face either Ivory Coast or Norway on Sunday in New Jersey in the round of 16.