Police in Milan say the arrest took place "in recent days."
When stopped by officers for a routine check, the man became "particularly aggressive," they added.
In a statement, police said the Algerian was "repeatedly shouting 'Allahu Akbar' while attempting to grab from his backpack an object that turned out to be a knife with a blade more than 12cm long.
The man was later found to be wanted by Algerian authorities, suspected since 2015 of belonging to "Islamic State militias and employed in the Syrian-Iraqi theatre of war," police said.
The suspect was unknown to Italian authorities and is currently in Milan's San Vittore prison as he awaits extradition, they added.
Jihadist group Islamic State, IS, proclaimed a "caliphate" in 2014 across swathes of Syria and Iraq, launching a reign of terror that continues with hit-and-run attacks and ambushes.