The Catalans were given the silverware when the final whistle was blown, as now they are eight points clear on top, while second-placed Real have only two matches to go.
Robert Pires put Villarreal ahead on 17 minutes and Rafael van der Vaart hit an equalizer 14 minutes from the hour mark.
Robin Cani restored the hosts' one-goal cushion on 62 minutes, before Gonzalo Higuain restored parity again two minutes from time.
Joan Capdevila hit the winner in the dying minutes to earn his team a hard-fought win and give Barcelona, who beat Athletic Bilbao 4-1 in the Copa del Rey final on Wednesday, their second trophy in a week.
Barca will complete a treble if they overcome defending champions Manchester United in the UEFA Champions League final in Rome on May 27th.
Stuttering
Real, who also lost in their two previous league games, seemed to be stuttering from the start, and the home side capitalized on their lack of confidence to take the lead.
Pires opened the scoring after heading an Ariel Ibagaza cross into the net.
Juande Ramos bolstered Los Merengues' frontline upon sending Higuain into battle.
The introduction of the Argentine striker paid off in no time, as his first touch was a low cross that set up Van der Vaart to score the leveler.
After Cani hit Villarreal's second, Higuain came in handy again and equalized with a late strike. But when Real were close to salvaging a point Capdevila notched a last-gasp stunner.
Real remain in second place with 78 points, and Villarreal advanced to fifth spot on 59 points.
نرشح لكم
أتلتيكو مدريد: إذا طبعنا الأخطاء التحكيمية لصالح ريال مدريد لنفدت الغابات
تقرير: ريان آيت نوري يقترب من الرحيل عن مانشستر سيتي.. ويوفنتوس يترقب
كالفريت لوين يحسم مستقبله مع ليدز
ماك أليستر: لا يوجد قرار حاسم بشأن مستقبلي مع ليفربول
أتلتيكو مدريد يرد على هجوم مورينيو على حكم دربي
نجل ماركيزيو يوقع على أول عقد احترافي مع يوفنتوس
تقرير: نيوكاسل وبرشلونة يتنافسان على موهبة النرويج الجديدة
كفاراتاسخيليا جديد؟ مدرب فروزينوني: والدتي المسنة أعجبت بمستوى كفيرنادزي












خبر في الجول – الزمالك يستقر على فسخ التعاقد مع الشركة التي تدير الكرة النسائية