The 12-time Grand Slam champion, who has won the title in Spain twice before, sealed a 6-1 4-6 7-5 victory in two hours and 17 minutes.
It was a difficult start for Djokovic as he lost his opening service game, but he bounced back in style by breaking Almagro's serve three times in a row.
After ripping through the first set in less than half an hour, Djokovic was unable to continue that momentum as Almagro saved four break points before snatching his opponent's serve and levelling up the match.
Both players dropped serve in the early stages of the third, but it was Djokovic who found that crucial break to seal victory and put himself into the next phase of the tournament.
The match was Djokovic's first since splitting from his coaching team of 10 years.