The Swiss, who held the Brazilians to a 1-1 draw in Russia four years ago, looked set to repeat the feat until the Manchester United midfielder struck seven minutes from time to claim the points.
Defeat was perhaps harsh on Murat Yakin's men, who had frustrated the five-times winners for long periods to make them feel the absence of injured superstar Neymar, although a point from their final fixture against Serbia would be enough to send them through unless Cameroon pull off a major shock against Brazil on Friday.
In a cagey start, neither side was able to exert any real control on a game fought out largely in the middle third of the pitch until Lucas Paqueta, Richarlison and Vinicius started to combine in ominous fashion.
Indeed, neither goalkeeper had a save of note to make until Yann Sommer had to dive to his right to turn away Vinicius' 27th-minute scuffed strike as he slid in at the far post to meet Raphina's cross.
Raphina drilled a curling left-footed shot into Sommer's midriff after cutting across the edge of the penalty area four minutes later, with Brazil pressing and the Swiss offering little in response.
Thiago Silva had to be at his best to prevent Ruben Vargas from turning on Ricardo Rodriguez's ball into the box six minutes before the break, but that foray deep into enemy territory proved something of a rarity during the opening 45 minutes.
West Ham midfielder Paqueta departed to be replaced by Real Madrid's Rodrygo at the break, but it was the Swiss who came out of the blocks the quicker with Alex Sandro having to block a Silvan Widmer cross and Vinicius, Djibril's Sow's shot before Vargas' 54th-minute attempt was deflected wide.
Switzerland failed to take advantage after Breel Embolo had charged down goalkeeper Alisson's clearance and Richarlison only just failed to get a touch on Vinicius' teasing cross as Brazil hit back.
Vinicius thought he had given his side a 64th-minute lead with a cultured finish after skipping away from defender Nico Elvedi and drawing Sommer, but his effort was ruled out for offside against Richarlison earlier in the move following a VAR review.
But they secured victory with seven minutes remaining when Casemiro blasted home a winner with the help of a slight defection off Manuel Akanji's backside after Vinicius and Rodrygo had combined to set him up.