Azhar's 139 helped guide Pakistan to 257 for three at stumps on Thursday's second day of the third Test in Birmingham, just 40 runs behind England's first innings 297.
Together with Sami Aslam (82), he helped rescue Pakistan from the depths of none for one after Mohammad Hafeez's fourth-ball duck.
Unfortunately for Pakistan, Ali was out to the very last ball of the day, caught at first slip by England captain Alastair Cook as Chris Woakes struck with the new ball on his Warwickshire home ground.
Earlier, 20-year-old left-hander Aslam, recalled in place of the struggling Shan Masood, was run out in sight of a maiden Test century following Azhar's poor call.
But given Pakistan's repeated top-order problem, Aslam and Azhar's second-wicket stand of 181 was a much-needed morale boost.
Anderson may have struck early to remove Hafeez but England's all-time leading Test wicket-taker found himself involved in unpleasant scenes with on-field umpires Bruce Oxenford and Joel Wilson.
Australia's Oxenford twice officially warned Anderson in one over for running on the pitch, with the Lancashire star risking being barred from bowling again in the remainder of the Pakistan innings if he receives another such caution.
But it was his initial exchange with West Indian official Wilson, who first flagged up the problem, that could yet land Anderson in trouble after he appeared to question the umpire's judgement and snatched his cap from him at the end of an over.
That, however, was of little concern to a delighted Azhar, whose innings left the match intriguingly poised, with the four-Test series all square at 1-1.
"As a batsman, you really need just to focus as much as you can on that," the 31-year-old Azhar told reporters after stumps.
"It's always a dream for any player to come in England and score a hundred," he added following his 10th century in 48 Tests.
"This being my first century outside Asia makes me proud.
"It could have been better had I survived the last ball."
Azhar lamented the manner of Aslam's exit 18 runs shy of what would have been a fairytale century.
"It was unfortunate that he got run out. I felt sorry for him and would have loved him to have reached a hundred," he said.
Meanwhile Anderson, for all his regret, was left facing the possibility of disciplinary action from match referee Richie Richardson, the former West Indies captain.
"I had a bad half-hour where I let things get to me," said Anderson.
"I've apologised to both umpires, because my behaviour wasn't acceptable.
"I know I was out of order for the way I behaved.
"It wasn't acceptable and it was petulant," added Anderson, reprimanded earlier this season for "disrespecting" Indian umpire S Ravi in the Lord's Test against Sri Lanka.
Anderson's mood would not have been helped when Joe Root dropped Azhar off his bowling at second slip on 38.
"It probably added to (the frustration), but you have to deal with things in cricket," said Anderson.
"You won't get every catch taken, and there will be times when the opposition play well - which they did today."
Azhar marked his century with the press-up routine and dressing room salute that have become Pakistan trademarks this series.
England have not always appreciated the gestures but Anderson said: "It's up to to them how they want to celebrate. I wasn't too fussed.
"Probably the look on my face was more of how the day was going than his celebrations."