The suspected attacker was shot and killed by police after the teacher died in the street in Conflans-Saint-Honorine.
French president Emmanuel Macron denounced what he called an “Islamist terrorist attack” and urged the nation to stand united against extremism.
Authorities said the teacher had discussed caricatures of Islam’s Prophet Muhammad with his class.
The French anti-terrorism prosecutor opened an investigation for murder with a suspected terrorist motive.
Four people, one a minor, were detained hours later, the office of anti-terror prosecutor Jean-Francois Ricard said. Police typically fan out to find family and friends of potential suspects in terror cases.
Mr Macron visited the school where the teacher worked in Conflans-Saint-Honorine and met with staff.
An Associated Press reporter saw three ambulances at the scene, and heavily armed police surrounding the area and police vans lining nearby streets.
Mr Macron said: “One of our compatriots was murdered today because he taught … the freedom of expression, the freedom to believe or not believe.”
He said the attack should not divide France because that’s what the extremists want. “We must stand all together as citizens,” he said.
The incident came as Mr Macron’s government works on a bill to address Islamist radicals who authorities claim are creating a parallel society outside the values of the French Republic.
France has the largest Muslim population in western Europe with up to five million members, and Islam is the country’s number two religion.
A police official said the suspect, who was armed with a knife and an airsoft gun – which fires plastic pellets – was shot dead about 600 yards from where the male teacher was killed after he failed to respond to orders to put down his arms, and acted in a threatening manner.
The teacher had received threats after opening a discussion “for a debate” about the caricatures about 10 days ago, the police official told The Associated Press.
The parent of a student had filed a complaint against the teacher, another police official said, adding that the suspected killer did not have a child at the school.
An ID card was found at the scene but police were verifying the identity. French media reported that the suspect was an 18-year-old Chechen, born in Moscow. That information could not be immediately confirmed.
France has seen occasional violence involving its Chechen community in recent months – in the Dijon region, the Mediterranean city of Nice, and the western town of Saint-Dizier – believed to be linked to local criminal activity.
It was not known what link, if any, the attacker might have with the teacher or whether he had accomplices.
The attack is second terrorism-related incident since the opening of an ongoing trial for the January 2015 newsroom massacre at the satirical newspaper Charlie Hebdo, which had published caricatures of the prophet of Islam.
As the trial started, the paper republished caricatures of the prophet to underscore the right of freedom of expression.
Soon afterwards, a young man from Pakistan was arrested after stabbing two people with a meat cleaver outside the newspaper’s former offices. They did not suffer life-threatening injuries.
The 18-year-old told police he was upset about the publication of the caricatures.
In a video posted recently on social media, a man describing himself as a father at the school said the teacher who was slain had recently shown an offensive image of a man and told students it was “the prophet of the Muslims”.
Before showing the images, the teacher asked Muslim children to leave the room because he planned to show something shocking, the man said.
“What was the message he wanted to send these children? Why does a history teacher behave this way in front of 13-year-olds?” the man asked.