Barca equalised just before half-time through Sergi Roberto’s shot after Raphinha’s effort had been blocked.
Marco Asensio saw a late strike for Real ruled out for offside, before Kessie scored the injury-time winner.
Both sides have 12 league matches remaining and Barca are in a dominant position as they look to win the title for the first time since 2018-19.
Roberto said: “You can never say [the title] is over but we have now gone 12 points clear so it’s difficult for them.
“It’s down to us and that’s the most important thing. I’m just glad to have helped the team with a goal. It was epic at the end and I’m pleased for Franck to get the goal.
“The game swung when we thought Asensio had scored, it was ruled offside and we scored. It feels even better when you win with a late goal and it’s fantastic to do so in front of our supporters.”
Reigning Spanish champions Real came into this game having beaten Liverpool 1-0 on Wednesday to complete a 6-2 aggregate victory to move into the Champions League quarter-finals.
Karim Benzema, who scored against the Reds, had the first shot of the match inside the opening minute but saw his effort from outside the penalty area well held by Marc-Andre ter Stegen.
But the visitors were then indebted to goalkeeper Thibaut Courtois as he made excellent saves to keep out Robert Lewandowski’s well-struck 25-yard effort and Raphinha’s header.
Real took a ninth-minute lead, but Barcelona, 26-time Spanish champions, had the better of the first half.
Andreas Christensen was unable to take two opportunities and Raphinha had a shot tipped over by Courtois before Roberto levelled in the 45th minute.
Lewandowski, the top scorer in La Liga with 15 goals, had a shot deflected just wide, before Real substitute Rodrygo fired over at the other end in his first involvement.
Raphinha nearly won it in the 74th minute but was denied by another excellent Courtois save while Lewandowski also shot into the side netting.
Asensio had only been on the pitch for six minutes when he put the ball in the net for Real late on, but the goal was ruled out after a tight video assistant referee offside decision.
Substitute Kessie was then Barcelona’s hero in the 92nd minute, finishing after good work from Alejandro Balde.
Barcelona manager Xavi said: “The league is not decided but it’s a huge win of course.
“I think we deserved the win and we were better than Real Madrid, we created more opportunities.”