The Virginia General Assembly ended its annual session Saturday with passage of a sweeping transportation deal, handing Gov. Robert F. McDonnell (R) a qualified victory on an issue that has vexed the state for a generation but that also puts him at odds with the conservative wing of his party.

On the last day of the legislature’s 46-day gathering, the Senate gave its blessing to a plan that dramatically overhauls the way Virginians will pay for roads, highways and mass transit — but not before Democrats also won a pledge from McDonnell on the Affordable Care Act’s planned expansion of Medicaid for poor and elderly people.

Soon after McDonnell wrote a blistering letter about his reluctance to expand the shared federal and state program because of growing costs, Democratic senators threatened to derail the $3.5 billion transportation measure unless McDonnell agreed, in writing, to honor their compromise on Medicaid.

He did, and despite a last-minute challenge Saturday from Attorney General Ken Cuccinelli II, the Medicaid deal held together well enough for the Senate to take up the historic transportation measure a day after the House passed it.

“This isn’t any bill, this is the only bill, and we did not reach this decision lightly without hundreds of hours of anguish and numbers-crunching,” said Senate Majority Leader Thomas K. Norment Jr. (R-James City), one of the transportation plan’s final negotiators. “It is the only solution we could come up with.”

The trick in Virginia has been to convince a public that dislikes taxes almost as much as it dislikes traffic jams that the way forward requires new revenue. The new plan would do so by replacing the 17.5 cents-per-gallon tax on gasoline — which had not been changed since 1987 — with a new 3.5 percent wholesale tax on motor fuels that will keep pace with economic growth and inflation. Supporters say the average motorist could pay as much $15 more a month.

The deal’s major components also include boosting the sales tax on nonfood merchandise from 5 percent to 5.3 percent and devoting a fatter slice of existing revenue to transportation instead of schools, public safety and other services. And it creates a regional funding mechanism that boosts the sales tax to 6 percent in Northern Virginia and Hampton Roads and requires those funds to be spent only on transportation projects in those areas.

To win passage, Republicans had to swallow their aversion to raising taxes, and Democrats had to accept diverting as much as $200 million a year in general fund revenue toward roads instead of schools or other services.

Supporters praised the plan to raise about $880 million a year, including the new dedicated streams of money for mass transit, while opponents spoke out against taxing different parts of the state at different rates or doubling the registration fee on electric cars to $100 and applying it to alternative fuel and hybrid vehicles, too.

“Why are we moving backwards on a deal that hurts nondrivers?” Sen. Adam P. Ebbin (D-Alexandria) wondered.

On other side of the aisle, Sen. Ralph K. Smith (R-Roanoke), who represents an area where unemployment is high and household budgets are stretched, said the bill sent an unpleasant message: “We’re going to take a bigger chunk of your wages in multiple ways.”

The Senate ratified the compromise by a vote of 25 to 15, with only eight Republicans voting in the affirmative. On Friday, the House passed the bill by a vote of 60 to 40, with the slimmest majority of the Republican caucus voting for it. The bill now goes to the governor.

McDonnell had challenged the legislature to adopt a new funding plan that would eliminate the per-gallon gas tax, raise the sales tax and take an even larger haul of existing revenue for roads. Lawmakers agreed, but they also heavily revised his initiative, increasing the amount of revenue it would raise through taxes.

“This is a historic day in Virginia,” McDonnell said in a written statement Saturday. “We have worked together across party lines to find common ground and pass the first sustainable long-term transportation funding plan in 27 years. There is a ‘Virginia Way’ of cooperation and problem solving, and we saw it work again today in Richmond.”

Though the transportation bill perhaps commanded the most attention, the 2013 legislative session’s 2,574 bills and resolutions also took action on school choice and teacher accountability, tougher requirements for voter ID, and upholding the status quo on relatively easy access to firearms. A school safety task force, convened by the governor after the mass killings at an elementary school in Newtown, Conn., led to a package of new measures, including stiffer penalties for people who buy guns illegally and additional money to make schools secure and paying for police officers to guard them.

But the governor lost in his bid to make it easier to restore voting rights to convicted felons, and several other high-profile bills also fell short, including a campaign to allow uranium mining in Virginia’s Southside, a proposed study to explore whether Virginia should establish its own currency if the U.S. dollar were ever to collapse, and a GOP-backed effort to redistribute Virginia’s electoral votes in presidential elections.

The General Assembly also agreed to revisions on the state’s $85-billion, two-year budget that embodied the compromise on Medicaid, boosted state employees’ and teachers’ salaries by about 2 percent, funded additional seats at state universities for in-state students, and began to pay back money borrowed from Virginia’s state employee pension fund.

The session got off on a less partisan note than in 2012, which opened with a power struggle in the 20-20 Senate and ended with a standoff on the budget. But harmony did not last long. Republicans muscled a Senate redistricting plan through that evenly divided chamber on Inauguration Day, capitalizing on the absence of a Democrat, who was attending President Obama’s swearing in.

House Speaker William J. Howell (R-Stafford) bucked his party to rule that the measure was out of order, thereby clearing the way to forge a compromise on transportation. But that too was nearly stalled by partisan feuding over Medicaid. Under a compromise finalized Saturday, the legislature will empower a commission with 10 legislators to oversee Medicaid expansion if certain reforms are achieved.

On education, McDonnell got much — but not all — of what he asked for this session.

Lawmakers endorsed McDonnell’s plan to give the Virginia Department of Education the authority to create an A-to-F grading system for the state’s schools so that parents might have a better way to judge schools’ quality. The General Assembly also approved McDonnell’s proposal to create a statewide school division, known as the Opportunity Educational Institution, that would take control of chronically underperforming schools.

Only a few of more than 1,800 schools in the state meet the criteria for takeover, including Alexandria’s Jefferson-Houston Elementary School. Under the plan, control of these schools would be handed to an appointed board. The proposal was strongly opposed by groups representing the state’s teachers and school boards, some of whom warned that its provisions would allow the state to transfer the schools in charter schools.

McDonnell also won passage of a measure, with some compromises, that would change the way evaluations of and grievances against teachers are handled, effectively weakening Virginia’s tenure-like job protections. Legislators also approved a measure to expand the Education Improvement Scholarships Tax Credits for donations given to underwrite private school scholarships.

But McDonnell was unable to win passage of a proposed constitutional amendment that would have made it easier to open charter schools in the state.

The defining issue of the session — and several others in the past 27 years — was transportation. The final compromise reflected give-and-take between the parties and also between regions, with the commonwealth’s most populous and urbanized regions in Northern Virginia and Hampton Roads seeking solutions to gridlock and the more rural areas reluctant to increase the expense of traveling their roads.

Jim Corcoran, president of the Fairfax County Chamber of Commerce, liked the regional funding mechanism, but not the new 3 percent tax on hotel stays in Northern Virginia, while environmental groups criticized the move to loosen the link between gas taxes and consumption that requires drivers to pay at the pump for roads.

“If you’re lowering the price of gas at the pump you are creating an incentive for people to drive more, which means you’re going to have more congestion and more pollution,” said David Dickson, a spokesman for the Sierra Club’s Virginia chapter.

Stewart Schwartz, the executive director of the Coalition for Smarter Growth, expressed concern that too much thought was given to raising money without examining the questionable priorities the administration has in mind for it.

“[T]here’s nothing in the bill that ensures more responsible spending by this administration,” Schwartz said.

Mike O’Connor, president and chief executive of the Virginia Petroleum, Convenience and Grocery Association, said dealers were wary of the new tax structure. He said that based on the current wholesale price of gasoline, which is about $3.30 a gallon, the new levy would add about 11.5 cents per gallon to the price of fuel.

Del. David B. Albo (R-Fairfax), who said the typical family will pay about $10 to $15 a month under the package, said new revenue was necessary. During the floor debate, he displayed a chart showing that Virginia’s per-capital funding has fallen to 2005 levels.

“If it were possible to build roads out of existing revenues, I’d prefer to do it,” Albo said after the vote. “But it’s mathematically impossible.”

Some Republicans agreed, relenting from their party’s anti-tax orthodoxy.

“What I thought I was going to do was make sure that I never, ever, ever, ever, ever, ever raised taxes because there are a few people back home who sent me here to do that,” Del. Thomas A. “Tag” Greason (R-Loudoun) said during the House debate. “But as I think about where we are today, and the 27 years we’ve been working on this problem, I think to myself that I was also sent here to solve problems. And the problems that we solve are not always the problems we want to solve.”

But other Republicans stood their ground, saying the transportation package relied too heavily on higher taxes when the state is running surpluses. Some agreed with Grover Norquist, president of Americans for Tax Reform, said McDonnell lost control of his transportation initiative in the legislature and, by agreeing to hike so many taxes, doomed his chances of running for higher office on a Republican platform.

“It’s exactly what the governor said he was not going to do,” Norquist said. “The Democrats in the legislature mugged him good.”