BOSTON — Republican Gov. Charlie Baker has vetoed legislation approved by Massachusetts lawmakers that would give nearly $18 million in annual pay raises to top legislators, statewide elected officials and judges.

Baker said it was the wrong time to sign off on an increase in pay.

“Given the commonwealth’s fiscal outlook as we continue to right size our budget, close the structural deficit, and reduce the reliance on one-time revenues without raising taxes we felt it was important to veto this fiscally irresponsible legislation,” Baker told reporters Friday.

Baker said the increase in pay would have put additional burdens on the state’s pension liabilities in the future.

The Democrat-controlled Senate voted 31-9 in favor of the legislation Thursday, a day after the House approved it by a 115-44 vote.

That’s a large enough margin in both chambers to override Baker’s veto.

Baker said his office has received hundreds of phone calls from residents concerned about the pay increase. He said the veto may at least give those residents and others time to call their state representative or senator to make their voices heard.

He said the vast majority of the calls to his office opposed the pay increase.

Democratic Senate President Stan Rosenberg has defended the increase, saying some lawmakers are being forced out of office because of the low pay.

“We are losing young people every election cycle.” Rosenberg told reporters Thursday after the Senate passed the bill. “Particularly the younger members who are trying to start families and start their career, they cannot live on this.”

The annual salary for Rosenberg and fellow Democrat House Speaker Robert DeLeo would climb about $45,000 to more than $142,000 a year.

Baker said anyone running for office in Massachusetts knew what the salary was when they decided to run.

“There are a bunch of folks who work in the Legislature who also have second jobs,” he said. “Many of them are lawyers.”

The bill wouldn’t change the $62,547 annual base pay for lawmakers, but would increase additional stipends paid to Democratic and Republican leaders and to the chairs of key legislative committees.

Other constitutional officers, including the attorney general and state treasurer, would also get substantial raises.