The far-right politician was attacked amid a crowd in the south-east state of Minas Gerais. Hospital officials said Jair Bolsonaro had surgery for injuries to his intestines and is expected to recover.
The controversial politician, who has outraged many in Brazil with racist and homophobic comments, has performed strongly in recent opinion polls.
The polls suggest he will get the most votes in next month’s presidential elections if former President Luis Inácio Lula da Silva remains blocked from standing.
Left-wing Lula had been the long-standing favourite but he is currently in prison, appealing against a ban on his candidacy that was imposed after his conviction for corruption.
Some of Bolsonaro’s past comments have caused uproar, including equating homosexuality with paedophilia, and saying a congresswoman was too ugly to be raped.