Henriette Reker – an independent candidate supported by Chancellor Angela Merkel’s CDU party – was seriously injured along with an aide. Three others suffered minor injuries.
Local police said Ms Reker, 58, was “stable, but not out of the woods”.
Police have arrested a 44-year-old German national and Cologne resident.
The attacker told police he stabbed Ms Reker “because of anti-foreigner motives,” senior police investigator Norbert Wagner said.
The suspect appeared to have acted alone and had no police record, Mr Wagner added.
Ralf Jager, a regional interior minister, said: “The first signs speak for a politically motivated act.”
The attacker will undergo a psychiatric examination to establish whether that was his primary motive or whether he had a health problem.
A spokesman for Ms Merkel said the chancellor “expressed her shock and condemned this act”.
Germany’s interior minister, Thomas de Maiziere, called the attack “appalling and cowardly”.
City officials said the election would go ahead as planned. Ms Reker has been the head of Cologne’s social affairs and integration department since 2010.
Ms Merkel has come under fire from some in Germany over the large numbers of refugees and migrants being allowed into the country.
Germany has said it expects 800,000 people to seek asylum this year, but a leaked report suggested the number could be as high as 1.5 million.
Politically-motivated attacks are relatively rare in Germany. In 1990, then-Interior Minister Wolfgang Schaeuble was shot while out campaigning, an attack that left him using a wheelchair.
A few months earlier, Oskar Lafontaine, then a prominent member of Germany’s main opposition party, was stabbed in the neck by a mentally disturbed woman.