The club has triggered an option to keep the 26-year-old, who was on loan from Egyptian side Al Ahly.
The Egypt international has signed a deal until summer 2022.
"This is great news for the club - Ahmed has been absolutely terrific for me since I arrived and I am sure there is even more to come from him in the future," said boss Alan Pardew.
"He is one of the players we can build around on the road ahead. He has a big role to play and it is good for the player and the club to remove any doubts and make the transfer permanent."