Barcelona police are treating a knife attack on officers at a city police station as a terrorist incident.
Commissioner Rafel Comes said officers shot the man dead after he entered the station in Cornella district and pulled out a knife.
Mr Comes said the man shouted "Allah" and other words that officers did not understand.
Police have found no evidence linking the incident to last year's terror attacks in Barcelona and nearby Cambrils, which killed 16 people.
Intelligence services are helping with the investigation.
Officers opened a locked police station security door to the man, who lived in a nearby apartment.
The suspect repeatedly pressed the buzzer to be let in at around 5.45am and spoke with officers inside over an intercom.
After they decided to let him in, he pulled out "a large knife" and lunged at officers, according to Mr Comes.
The man had ID documents, and police are checking whether they are authentic.
Mr Comes said authorities have found no criminal record for the man identified in the documents, although international police databases are being searched for matches with his fingerprints.
A search of the man's apartment has been planned.