The attacker was arrested immediately and identified as Adélio Bispo de Oliveira, 40 – said to have been a member of the left-leaning PSOL party.
After his arrest, Bispo de Oliveira said he was "carrying out a divine mission, a mission from God," said Luis Boundens, head of a union of federal police officers. Authorities are investigating the suspect's mental health, he added.
Bolsonaro, a former military man and lawmaker, has been criticised for outbursts deemed racist, misogynist and homophobic.
Bispo de Oliveira acted "for religious reasons, for political reasons, and also because of the prejudice Bolsonaro has always shown when he talks about race, religion and even women," said his lawyer, Pedro Augusto Lima Possa.
On his Facebook page, the attacker recently posted messages criticising Bolsonaro and supporting the government of President Nicolás Maduro in Venezuela.
President Michel Temer quickly condemned the attack and instructed his Minister of Security Raul Jungmann to reinforce security for candidates and conduct "a rigorous investigation," a spokesman for the presidency told AFP.
"It is intolerable to see that in a democratic state it is not possible to have a normal campaign," said Temer.
While the frontrunner in Brazil's presidential election is hospitalized, the most popular candidate, former president and leftist icon Luiz Inácio Lula da Silva, is in jail.
The Supreme Court on Thursday upheld a ban on Lula running in the election. First round voting is scheduled for October 7.
With Lula ruled out of the election, the latest polls from the Ibope Institute put Bolsonaro in the lead with 22 percent compared with 12 percent each for environmentalist Marina Silva and center-left runner Ciro Gomes.
One of Bolsonaro's campaign pledges has been to legalise the carrying of weapons in order to combat rising violent crime.
Despite being a long-serving member of Congress, Bolsonaro has successfully presented himself as an outsider, untouched by the corruption scandals engulfing so much of the political elite.
Perhaps the message that carries furthest is Bolsonaro's push for a harder crackdown on crime – in a country where police are already often engaged in low-level wars against gangs. About 64,000 people die in homicides every year.