Leandro Barbosa scored 23 points as Brazil used a dominant fourth quarter to defeat Spain, 88-82, on Monday.
Marquinhos Vieira Sousa added 13 points and Tiago Splitter finished with 11 for Brazil, which improved to 4-1 and took second place in Group B. The Brazilians will face Argentina in Wednesday's quarterfinals.
Pau Gasol netted a game-high 25 points to go along with seven rebounds for Spain, which fell to 3-2 and finished third in Group B. The Spaniards will meet France in the quarterfinals.
Marc Gasol had 20 points while Serge Ibaka contributed 14 points and six rebounds in defeat.
Brazil entered the fourth quarter trailing 66-57 after not holding the lead at any point in the contest, but dominated the final frame to earn the win.
Spain opened the scoring in the fourth when Felipe Reyes went 1-for-2 from the foul line, but Brazil answered with an 18-6 run to go on top, 75-73, on Barbosa's three-pointer with just over four minutes to play.
Pau Gasol put an end to the spurt with a basket at the other end to knot the game at 75 before Brazil reeled off nine of the next 10 points to take an 84-76 advantage with 1 1/2 minutes left in the game.
Spain would get no closer than five points from there.