The Selecao took the lead after four minutes, Barcelona's Daniel Alves providing the assist for Real Madrid's Marcelo to net with a fine cross-shot.
Ronaldinho then went close from a free-kick, but Safet Susic's side equalised in the 12th minute through Vedad Ibisevic. Manchester City striker Edin Dzeko intercepted a poor pass from David Luiz that had been intended for Sandro, and fed Ibisevic. He tried his luck from long range and Brazil goalkeeper Julio Cesar was unable to keep it out.
The South Americans pushed forward looking to re-establish the lead and mustered the only clear chance in the remainder of the first half, but midfielder Hernanes was thwarted by a late tackle from defender Emir Spahic.
Brazil did not overly trouble the Bosnian defence in the second half, although man of the match Neymar brought a decent save out of keeper Asmir Begovic and then in the final minute the winner arrived.
Paulo Henrique Ganso sent a cross into the heart of the box and defender Papac inadvertently directed it past his own keeper.