behind which a group of masked military police officers were protesting when he was hit by two bullets in the town of Sobral.
He is in hospital in a stable condition.
For months, members of the military police have been conducting strikes and protests to demand pay increases.
Video shared on social media shows Senator Gomes driving a digger at the metal fence and lifting it up with the digger's arm while masked policemen gathered behind it shout.
Several shots ring out and the windscreen of the digger and one of its windows shatters.
Photos taken subsequently show Mr Gomes being helped away, his T-shirt showing a large blood stain.
It is not clear who fired the shots.
Why the stand-off?
Members of the military police in Sobral, in the north-eastern state of Ceará, have been demanding better pay since late 2019 and holding strikes.
They argue that they are being paid less than their counterparts elsewhere.
Police officers are banned from going on strike under Brazilian law and a court in Ceará ruled this week that those defying the ban could face prison.
As part of their protest, the officers barred the entrance to a military police station in the town to impede other members of the force who have not joined their strike from working.
Senator Gomes organised a counter-protest and warned the officers to clear the road. He told them over a megaphone that they had five minutes to clear off.
When they did not, he got into the cab of the digger and drove towards the fence behind which they were gathered.
What has been the reaction?
Public Safety Minister Sérgio Moro has announced the deployment of federal security forces to Ceará in the wake of Senator Gomes' shooting. Mr Moro said that they would stay for at least 30 days.
Senator Gomes' brother, Ciro Gomes, said that he "hoped that the responsible authorities catch those who attempted this barbarous homicide". He also wrote that he and his brother would not let the state be run "by militias".
Ciro Gomes is an influential politician in Ceará who has unsuccessfully run for the presidency of Brazil on three occasions.
The son of President Jair Bolsonaro criticised Senator Gomes for driving at the police officers. Eduardo Bolsonaro, who is a member of Congress, asked what reaction Mr Gomes had expected when he drove the digger towards the officers.