Defender Clement Lenglet scored the winning goal and was sent off as Barcelona beat Real Betis to close the gap on La Liga leaders Real Madrid.
The champions twice came from behind to level before Lenglet headed in Lionel Messi's 73rd-minute free-kick.
Sergio Canales' sixth-minute penalty gave Betis an early lead but Frenkie de Jong levelled three minutes later.
Nabil Fekir's fine strike restored the hosts' advantage only for Sergio Busquets to make it 2-2 at half-time.
Trailing 3-2, Betis were reduced to 10 men after Nabil Fekir received two yellow cards in quick succession but Lenglet's dismissal made for a tense finale.