Milan twice came from behind as Zlatan Ibrahimovic, who scored against his former club, and Kevin-Prince Boateng replied to a Mark van Bommel own goal and a twice-taken Lionel Messi penalty.
Both teams had already qualified for the last 16 with two matches to spare. Barcelona’s win put them five points clear at the top and ensured they won Group H.
Boateng’s stunning goal early in the second half was the pick of the five but Xavi’s 63rd-minute goal decided the game.
Barca quickly imposed their passing game on the hosts and went in front after 14 minutes when Messi found Seydou Keita at the far post and the Malian’s low ball back across was turned into his own goal by Van Bommel.
Milan occasionally looked dangerous and, after Robinho missed a glaring chance from close range, Ibrahimovic levelled when he turned Clarence Seedorf’s pass beyond Victor Valdes.
Barca regained the lead in the 31st minute when Messi converted a harsh penalty awarded for a foul on Xavi at the second attempt, his first having been disallowed for a dummy at the end of his run-up.
That practice was recently banned by FIFA and earned Messi a yellow card.
Milan replied again in the 54th minute when Boateng controlled the ball and flicked it past a bewildered Eric Abidal in one movement, then fired a shot between Valdes and the near post.
Not to be outdone, Barca replied with an equally outstanding goal nine minutes later.
Messi threaded the ball through a pack of defenders with stunning geometrical precision and Xavi took it in his stride to slot the ball past Christian Abbiati.