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.
Read:
Merkel condemned the attack, which 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 end of the year.
Read: Fears mount in Germany over record migrant influx: poll
The chancellor's open-door policy has provoked a backlash among her conservative allies and sparked protests among the far-right.
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".
Merkel "expressed her shock and condemned this act," a spokesperson for her office told AFP, while Interior Minister Thomas de Maziere slammed what he called an "appalling, cowardly" attack.
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. BBC reports the attacker told police he stabbed Ms Reker "because of anti-foreigner motives," senior police investigator Norbert Wagner said.
Reker was said to be in a stable condition after the attack, which came at a CDU information stand in a market in the city. Four other people were wounded in the incident, one seriously.
Read: Germany's SAP working on migrant registration app
In the evening, Cologne residents and politicians formed a human chain outside the city hall.
"The human chain is a sign that we stand together," said Hannelore Kraft, the head of North Rhine-Westphalia state where Cologne is located.
"We are here... to send a message in the wake of this abominable act," she said.
Justice Minister Heiko Maas condemned the attack as "an unimaginable and abominable act" while regional president Annelore Kraft said it was an "assault on democracy."
Read: Planned German migration centre hit by arson attack
Reker is seen as standing a good chance of securing the mayorship Sunday in Cologne, Germany's fourth-largest city with 980,000 inhabitants.
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ