Lionel Messi scored twice as Barcelona saw off Real Sociedad at the Camp Nou to keep up the pressure on La Liga leaders Real Madrid before next week’s Clásico, but they had to work hard for their victory.

With Madrid having earlier beaten Sporting Gijón after a late goal from Isco, the tension was firmly back on Luis Enrique’s side who had lost their last two matches.

Messi drilled in the opening goal from 25 yards after 17 minutes and then added a somewhat fortunate second when the ball bounced in off his leg from a rebound. Sociedad reduced the deficit through an own goal by the Barcelona defender Samuel Umtiti only for Paco Alcácer to quickly restore the hosts’ advantage.

However, a neat finish by Xabi Prieto in first-half stoppage time set up a tense second period, as Enrique’s team eventually got themselves back to winning ways as they prepare for two matches that could define their campaign.

Barcelona were looking to recover from the 3-0 Champions League quarter-final first leg defeat by Juventus that came on the back of last weekend’s unexpected loss at Málaga, where Neymar was sent off and handed a three-match domestic ban.

The hosts nearly found themselves trailing inside the first few minutes on Saturday when Mikel Oyarzabal’s goalbound shot was cleared off the line by Gerard Piqué. Barcelona were soon on the offensive though, with Luis Suárez stumbling in the penalty area as he tried to get on the end of a low cross from Jordi Alba.

The Sociedad keeper Gerónimo Rulli was quick off his line to make two important saves, first as Alcácer got into the right side of the penalty area and then when Sergi Roberto latched onto the rebound.

Messi, though, gave Rulli little chance when he drilled in the opening goal. The Argentina midfielder collected Suárez’s square ball 25 yards out and sent a fierce drive into the bottom right corner.

The visitors found themselves further behind on 37 minutes as Messi knocked in one of the most fortunate goals he is likely to score as he closes in on 500 for Barcelona. Rulli saved Suárez’s angled shot at the near post, the rebound fell into Messi’s path and the ball hooked off his trailing leg into an empty net.

Sociedad, aiming to close the gap on the European places, reduced the deficit in the 42nd minute. The Barcelona keeper Marc-André ter Stegen saved a header from Willian José and Iñigo Martínez was first to the loose ball, which he pulled across goal and it cannoned in off the defender Samuel Umtiti.

The home side quickly restored their two-goal cushion when Alcácer was played into the left side of the Sociedad penalty area by Messi and drilled an angled shot past Rulli.

However, Barcelona paid for another lapse in concentration in first-half stoppage time. José clipped an angled pass across to Prieto who had ghosted into the right side of the penalty area and dispatched a cool sidefoot finish into the bottom corner.

After such a frantic finish to the opening period, there was a dip in tempo at the start of the second half. While Sociedad were happy enough to try to contain Barcelona, they also remained a threat on the counter.

As the hour mark approached, José found himself in space on the edge of the penalty area, but could not get his shot away.

The Barcelona captain, Andrés Iniesta, replaced Andre Gomes with 20 minutes left as Enrique aimed to inject fresh life into his team. On 72 minutes, Ter Stegen was almost caught out by a long-range lob from Asier Illarramendi, which the German tipped onto the crossbar.

In stoppage time, Rulli denied Suárez in a one-on-one as Barcelona at least regained a winning mindset before the coming week’s key matches.