SAO PAULO, Brazil - In one of the worst mass shootings in Brazil's recent history, a gunman opened fire inside a cathedral outside the country's most populous city of Sao Paulo, killing four people on Tuesday.

The gunman reportedly entered the Metropolitan Cathedral in Campinas city at 1 pm (15.00 GMT) on Tuesday, when a midday service was ongoing and sat in the back with some parishioners.

Moments later the man reportedly opened fire on the worshippers inside the church, firing at least 20 shots, targeting parishioners at random, even as they scrambled to exit the church.

In a statement, the sheriff investigating the case, Hamilton Filho told reporters that the gunman "did not arrive shooting," but that he watched the service from the back, before firing on parishioners.

Officials said that soon after the gunman opened fire, the cops deployed outside rushed into the church after hearing the shots.

Investigators revealed that the man had fired a revolver and a .38-caliber pistol inside the cathedral, before heading towards the altar and killing himself.

The gunman killed four worshippers and left four others with serious injuries.

While officials are still trying to establish the motive of the shooting, they managed to identify the gunman during the preliminary probe, pointing out that the shooting appeared to be premeditated.

The gunman was identified as 49-year-old Euler Grandolpho, a resident of Valinhos, a town near Campinas, who was caught in the act on video surveillance footage.

According to Sheriff Filho, Grandolpho shot one of the policemen in the leg, before shooting himself.

Later, investigators found 28 more bullets in his backpack.

While four people were declared dead on the spot, four others suffered grave injuries and were rushed to the hospital by emergency services.

In a video statement posted on Facebook, Father Amauri Thomazzi, who was the priest performing the Mass asked for prayers for the victims and the shooter.

Father Thomazzi said, "I had started to celebrate mass at 12:15 pm and at the end of the mass, a man entered and shot at his victims. Nobody was able to do anything. We are very shocked with what happened. I ask for everyones prayers."

Meanwhile, in an official statement released later on Tuesday, the Security Secretariat for the state of Sao Paulo said, "A man opened fire randomly on people inside until police intervened and shot at the gunman who then killed himself."

The statement also confirmed the toll, saying that in addition to the gunman, four other people died and four others were wounded.

Further, the Archdiocese of Sao Paulo posted a statement on its Facebook page and said that the killings provoked "deep pain."

It added that the cathedral would be closed for as long as required for the police investigation.

The shooting, which was dubbed as one of Brazil's worst mass shootings in recent times, comes at a time when the country is dealing with the rising casualties from gun violence.

While Brazil is one of the most violent countries in the world and witnesses rampant gun violence, mass shootingsare very rare in the country.

According to the Forum for Public Security monitoring group, 70 percent of murders in Brazil are committed with firearms.

In 2017, the country registered a record 63,880 murders and its homicide rate stands at 30.8 per 100,000 inhabitants.

The UN claims that the homicide rate in Brazil is three times higher than the level that the world body considers to meet the definition of "endemic violence."

Tuesday's incident also comes months before Brazils legislature is scheduled to vote on a controversial bill to loosen gun laws.

Brazil's right-wing President-elect Jair Bolsonaro, who is a former army captain and is set to take office on January 1, has pledged to crack down on crime.

During his campaign, Bolsonaro vowed to make it easier for average citizens to arm themselves.