Henriette Reker, an independent close to the ruling Christian Democrats (CDU) of Chancellor Angela Merkel, suffered serious wounds to the neck in the attack in the western city of Cologne, which is to elect its mayor on Sunday.
The attack comes as Germany struggles to cope with a huge influx of asylum seekers, whose numbers are expected to reach between 800,000 and a million by the year's end.
Reker was "responsible for taking charge of refugees" in the city, regional police head Wolfgang Albers said in a statement, suggesting the stabbing was a "political act".
The attacker, a 44-year-old unemployed man arrested at the scene, "said he had a racist motivation for committing this act", Cologne police official Norbert Wagner added at a news conference.
More From This Section
Reker was said to be in a stable condition after the attack which took place as she manned a CDU information stand at a market in the city. Four other people were wounded in the incident, one seriously.
Reker is seen as standing a good chance of securing the mayorship Sunday in Cologne, Germany's fourth-largest city with 980,000 inhabitants.