A suspected Islamist militant wounded two policemen with a knife near the parliament in Tunis on Wednesday and was later arrested, Tunisia's Interior Ministry said.
It was a rare incident in a country that has improved security since deadly attacks in 2015.
The assailant was known to authorities and said after the attack that he considered the police to be "tyrants", the statement said.
One of the policemen was taken to hospital for treatment after being wounded in the neck, while the other was only lightly wounded, it said.
"I saw a young man with a thick beard trying to kill a policeman. He put the knife in his neck before he was pursued by a second policeman," a witness told Reuters. "He shouted 'Allahu Akbar' (God is greatest) as he attacked the policemen."
The Interior Ministry said Wednesday's suspect had confessed that "he had adopted Takfiri thought three years ago and believes that killing security forces is a form of jihad".
"Takfiri thought" refers to a view that Muslims should proclaim other Muslims to be infidels and justify attacks against them.
Large numbers of police were deployed to the Bardo square in the aftermath of the attack, a witness said. Blood could be seen on the ground in the square.
The square is opposite the parliament building and close to the Bardo museum, where 21 people, mostly European tourists, were killed in an attack by three gunmen in March 2015.
A security source told Reuters the attacker was 25 and from Ettadamen - one of the largest, poorest suburbs of Tunis.
"I know him well, he is the youngest in a family of six brothers including a sister who works in the parliament's administration", Hamid one of the attacker's neighbours told Reuters.
Colonel Major Khelifa Chibani of Tunisia's national guard named the attacker as Zied Gharbi. Security forces raided his house and confiscated his personal computer and other items, he said.
Sofian Sliti, a spokesman for judicial counter-terrorism investigations said that "the attacker planned to join terrorist groups in Libya".
As well as the Bardo killings, Tunisia suffered two other major attacks in 2015, one against tourists at the beach resort of Sousse and the other against presidential guards in the capital.
The 2015 attacks severely damaged the economy, which has a large tourism sector at its Mediterranean beaches. Since then, security has been boosted at strategic sites while authorities have cracked down on militants, dismantling dozens of cells.
Tunisia was the only Arab country where a long-serving leader was toppled in the region's 2011 uprisings without triggering civil war or large-scale violence. Tunisian democracy activists who kept dialogue open between Islamists and secularists were awarded the Nobel Peace Prize in 2015.