Japan, the 2012 silver medallists, were staring at a second straight defeat in Group C that would have left them in serious danger of a first-round exit after Jhennifer put Brazil ahead early in the second half.
But captain Saki Kumagai rolled in a 92nd-minute penalty to equalise at the Parc des Princes in Paris before 19-year-old substitute Tanikawa's incredible first-time shot from 30 metres sailed over the goalkeeper and in for the winner. The result means Japan and Brazil - silver medallists in 2004 and 2008 -- are now level on three points after two matches.
World Cup holders Spain also have three points before playing Nigeria later in Nantes. The top two teams in each of the three groups advance to the quarter-finals along with the two best third-placed sides.
Japan squandered a first penalty in stoppage time at the end of the opening half, as Mina Tanaka's weak kick was easily saved by Lorena in the Brazil goal after Rafaelle was penalised for handball.
Brazil then went ahead on 56 minutes as veteran star Marta, playing at her sixth Olympics, sent a superb ball in behind for Ludmila who teed up Jhennifer to fire in. It looked as though that would be that - but Japan won another spot-kick for handball following a VAR review, this time by Yasmim, as the game went into injury time.
Kumagai made no mistake from the spot to level matters for Japan, who lost 2-1 to Spain in their first outing. But Japan were not finished, as Tanikawa pounced on a loose pass by Rafaelle to score the jaw-dropping winner.
Elsewhere Colombia beat New Zealand 2-0 in Lyon in Group A, in which hosts France play reigning Olympic champions Canada later.
The Canadians are in turmoil after being deducted six points on Saturday for the spying scandal which led to coach Bev Priestman being suspended for a year by FIFA. Record four-time gold medallists the United States take on Germany in Marseille in Group B.