A suspect, identified by authorities as 40-year-old Adelio Bispo de Oliveira, was arrested within seconds.
Police did not give a motive, but one official said the man appeared to be mentally unstable.
"Our agents there said the attacker said he was on a mission from God," Luis Boudens, president of the National Federation of Federal Police, said. "Their impression is that they were not dealing with a mentally stable person."
After more than four years of revelations of widespread corruption within Brazil's political class, anger is running high in the country, and analysts initially predicted this would be a change election. But no true outsider has emerged.
Instead, Bolsonaro, despite being a congressman since 1991, has harnessed much of the anger and presented himself as a maverick who will clean up a corrupt system. He also promises to confront a surge in crime, in part by giving police a freer hand to shoot and kill while on duty.
The public's anger is partially responsible for making this year's campaign the most unpredictable in years for Brazil, and the attack could lead to another seismic shift. The man leading polls, former President Luiz Inacio Lula da Silva, has been barred from running by electoral authorities because he was convicted of corruption and is in jail. That puts Bolsonaro in the lead position, though it is unclear how the attack might affect the campaign for the October 7 presidential ballot.
In the hours after the attack in Juiz de Fora, about 200km north of Rio de Janeiro, Bolsonaro supporters predicted it would carry him to the presidency.
"They made Bolsonaro a martyr," said Jonatan Valente, a student who joined a small vigil for Bolsonaro in Sao Paulo. "I think the left shot itself in the foot because with this attack they will end up electing Bolsonaro."
But it is unknown when he can get out again on the campaign trail and if his injuries will impede his ability to campaign.
- AP