Real Madrid s Austrian defender David Alaba celebrates scoring his team s second goal during the Spanish league football match between UD Almeria and Real Madrid (AFP)
Carlo Ancelotti's side trailed to an early goal by Largie Ramazani away against their newly-promoted opponents, but Lucas Vazquez equalised just after the hour mark.
The winner arrived with 15 minutes remaining at Almeria's Power Horse Stadium, as Alaba strode onto the pitch and curled a free-kick in off the upright after Luka Modric had been fouled just outside the box.
Real were made to work hard for the points against an Almeria side who won last season's second-tier title to return to La Liga after a seven-year absence.
At one point it looked like Madrid might suffer a defeat in their opening league game of the season for the first time since 2008, with Ancelotti's decision to rotate his squad not initially paying off.
The Italian had made five changes to the line-up that started in Wednesday's 2-0 win over Eintracht Frankfurt in the UEFA Super Cup in Helsinki, when goals by Alaba and Karim Benzema gave the defending European champions the first silverware of the campaign.
Alaba and Modric were among the players dropped to the bench, while close-season signings Antonio Rudiger and Aurelien Tchouameni were handed first competitive starts for their new club.
Yet the game was just six minutes old when former Chelsea defender Rudiger was caught out by a ball over the top and Ramazani ran through to beat Thibaut Courtois.
Ramazani, the 21-year-old Belgian winger who used to be on the books at Manchester United, ran off to celebrate in acrobatic fashion as Saudi-owned Almeria enjoyed a dream start to their top-flight comeback.
Yet the hosts needed some inspired goalkeeping from Fernando Martinez to keep them ahead into half-time, with the standout save coming low to his left to deny Rudiger five minutes before the break.
Vazquez then had the ball in the net in the 43rd minute only to be denied by a very tight offside call, but the full-back was the man who dragged the away side back level in the 61st minute as he pounced on a loose ball in the area to score.
Almeria, who had already seen a hanging Vinicius cross strike the frame of their goal on its way behind before that, soon conceded again.
When Modric was fouled just outside the box by Rodrigo Ely, Austrian defender Alaba was waiting on the sideline for the chance to come on.
The break in play allowed him to replace Ferland Mendy, and he came straight on to take the free-kick and score in stunning fashion to complete the latest comeback from Ancelotti's team.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO_Sports and on Facebook at AhramOnlineSports.)