Lionel Messi produced yet another masterclass as Barcelona marked the end of Luis Enrique’s Camp Nou reign by lifting the Copa del Rey.

Barcelona became the first team since the early 1950s to lift the cup three seasons running as Messi masterminded a 3-1 victory at the final game to be staged at Atlético Madrid’s Vicente Calderón home.

Messi set Barcelona on their way with his 54th goal of the season and Neymar and Paco Alcácer were also on target in the first half, with Theo Hernández offering Alavés brief hope with a wonderful free-kick.

Alavés had waited 96 years to reach a Copa del Rey final, and they did not disgrace themselves at the end of a season in which they finished ninth in La Liga after winning promotion.

But, as is so often the case, they had no answer to the brilliance of Messi and, after a season in which Barcelona have fallen short of their high standards, the favourites ended with a trophy. A ninth piece of silverware was a fitting end to Enrique’s three-year spell in charge of Barcelona.

Messi sounded his intent with a brilliant diagonal pass that almost set up Jordi Alba for a sixth-minute opener. But Barcelona’s momentum was slowed by a nasty ninth-minute clash of heads between Javier Mascherano and Alavés’s on-loan midfielder Marcos Llorente, which led to the former leaving the field on a stretcher. With talk that the Argentina defender is set to leave Barcelona this summer after seven years at the club, it would represent a sad end to Mascherano’s trophy-filled time in Catalonia if that was the case.

Barcelona had a hold on possession, but chances were limited as Alavés stifled the midfield and denied Messi space around the fringes of the penalty area. And Alaves nearly struck against the run of play when Ibai Gómez intercepted Gerard Piqué’s pass out of defence.

Gómez’s shot squirmed under the body of Jasper Cillessen and, when it hit the back of the Barcelona goalkeeper on the way out, it rolled agonisingly along the line.

Barcelona made the most of their good fortune to take the lead three minutes later on the half-hour mark. Messi played a clever one-two with Neymar before curling the ball into the corner of the net from 18 yards.

But Alavés equalised within three minutes as Hernández, on loan from Atlético and on familiar turf, struck a sensational 25-yard free-kick past Cillessen.

Messi and Ivan Rakitic went close to restoring Barcelona’s lead, but Alaves’s hopes of reaching the break level were ruined in a cruel three-minute spell. A sweeping team move released the substitute André Gomes and his cross was turned home by Neymar, scoring for the third successive Copa del Rey final despite looking in an offside position.

Worse was to come for Alavés in the third minute of stoppage time as Messi dribbled his way past several defenders before allowing Alcácer to finish neatly.

The Alavés goalkeeper Fernando Pacheco prevented Alcácer from claiming a second soon after the restart, and the Basque side refused to surrender in a competitive contest. Gomes blocked Ruben Sobrino’s goalbound shot and Cillessen stuck out a foot to stop Rodrigo Ely’s stabbed attempt embarrassing him at the near post.

But Barcelona saw the game out to claim a third straight Copa del Rey and some tangible reward after Real Madrid’s La Liga triumph.