Barca sit 44 places ahead of Ibiza and Madrid 56 above Unionistas but the lowly duo both came close to extraordinary upsets, only for Ibiza to lose 2-1 and Unionistas 3-1 in front of their jubilant home fans.
With Lionel Messi rested and not even on the bench, Antoine Griezmann proved the saviour for Barcelona, scoring twice in the second half, including a 94th-minute winner, after Pep Caballe had given the hosts a fairytale lead at Can Misses.
Discomfort was more fleeting more Madrid, who conceded an equaliser in the second half following a brilliant run and finish from Unionistas' Alvaro Romero, only to restore their advantage five minutes later.
Juan Gongora turned the ball into his own net after Gareth Bale, back from injury and illness, scored his first goal for his club since September 1.
Bale has again been linked with a move away from Madrid this month and his celebrations were notably muted.
But at the end, Unionistas fans were waving their scarves with pride just as Ibiza's supporters had applauded their team off, even though Griezmann had dashed their hopes just moments before.
Only last week, Barcelona topped Deloitte's annual Football Money League with a total revenue of $959.3m for 2018/19 while Madrid were just behind, the world's second richest club with $864m.
Unionistas, meanwhile, only came into existence in 2013, launched by their own supporters after UD Salamanca went out of business.
It was a dream draw to get Real Madrid but they almost had to move grounds because their Las Pistas one stand and lighting that needed upgrading.
Ibiza are even younger, having formed in 2015 and played in the fifth tier only three years ago.
La Liga chief concerned reforms will hasten growing financial gulf
Griezmann's rescue act was cruel but important for Barcelona's new coach Quique Setien, who enjoyed a 1-0 victory in his first game in charge against Granada on Sunday but could hardly afford a loss of this magnitude so early in his tenure.
For all the dominance of the ball, there was a lack of cutting edge against Ibiza, who restricted their star-studded opponents to one shot in the first half, Barca's lowest in any game they have played this season.
With a trip to Valencia to come in La Liga on Saturday, Setien left Messi, Sergio Busquets and Gerard Pique out of the squad. Youngsters Ansu Fati, Riqui Puig and Carles Perez all started.
Ibiza were at their visitors from the off, closing the gaps and snapping at their heels. If the home side's aim was to contain, they went one better by taking the lead in the ninth minute.
Puig was at fault, caught napping by Caballe, who finished off Javi Perez's cross.
Twice Ibiza almost added a second as Angel Rodado scored after bundling through Clement Lenglet but too enthusiastically, with the goal chalked off. Then Rai Nascimento hit the post after being played in by Javi Lara.
Ibiza's pressing lost intensity in the second half and in the 72nd minute, Griezmann equalised. Frenkie de Jong's reverse pass was perfectly weighted, with Griezmann able to hit first time on the turn.
Ibiza looked set for an extra 30 minutes but Griezmann struck again. Jordi Alba slid him through down and his finish flew across German Parreno and found the far corner.
Barca considering signing a striker: Setien
Bale announced his return to the Real Madrid line-up with a goal as his right-footed effort took an awkward deflection on its way in, even if there was little sign that he enjoyed it.
In the 57th minute, Romero, who was playing in the Spanish third tier last season, capitalised on a slip from Eder Militao to weave through and apply an excellent, lifted finish but parity was brief.
Gongora turned the ball into his own net under pressure from Brahim Diaz, who then added a third in injury-time.
Elsewhere, holders Valencia beat Logrones 1-0 while Segunda strugglers Tenerife knocked out La Liga's Real Valladolid with a surprise 2-1 win.
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ