Pep Guardiola's side had led through goals by Xavi and Lionel Messi inside the opening 12 minutes at Camp Nou before Ruben Castro pulled one back after half an hour and Roque Santa Cruz made it 2-2 in the second half.
The visitors had Mario sent off with 19 minutes remaining for a second booking before Alexis Sanchez's shot restored the lead 15 minutes from time and Messi made it 4-2 with a late penalty.
The hard-fought win ensured Barca remain five points off leaders Real Madrid.
"The opposition gave us problems, this is normal," said Guardiola. "But I saw nothing from my team to suggest they do not have the fight in them to continue to compete for the title."