In a coup for Mayor de Blasio and his progressive agenda, ultraliberal Melissa Mark-Viverito was elected speaker of the City Council Wednesday in a unanimous vote that shifted city government even further to the left.

An immediate sign that the political landscape had changed dramatically came when Councilman Ydanis Rodriguez (D-Manhattan) took to the floor to advocate closing the gap between the rich and the poor and proclaimed: “Long live the Wall Street occupy movement!”

The win makes Mark-Viverito, 44, the first Hispanic to gain the second-most powerful citywide post — and makes her the most liberal speaker in decades.

“I hope that as young Latinas and Latinos are witnessing this moment, they are able to dream that much bigger and are inspired to work that much harder, because we have broken through one more barrier,” said Mark-Viverito, whose two brothers and mother flew from Puerto Rico to witness the historic moment at City Hall.

The new speaker, who represents East Harlem and part of The Bronx, later acknowledged that she hadn’t known until the last moment whether her persistent opponent, East Side Councilman Dan Garodnick, would concede or stage a fight to the bitter end.

Garodnick’s group of roughly 20 backers had notified Mark-Viverito just moments before entering City Hall — and only after getting assurances they wouldn’t be punished or shut out from choice committee chairmanships — that he was giving up.

She was seen letting out an audible sigh of relief when his crew finally entered the council chambers nearly a half-hour late.

“We were given no specific guarantees, but a guarantee they’d work with us,” said one Garodnick supporter. Several councilmembers said the Bronx and Queens Democratic leaders who backed Garodnick were also assured a number of their members would keep current committee chairmanships, which comes with pay bonuses and extra political clout.

But most of those involved insisted the concession deal came so late that the terms had to be kept general. “They were told they’ll get their fair share, but with the understanding they came to the table late,” said one Mark-Viverito backer.

At the meeting, members from both sides made a concerted show of unity, with all 51 voting for the victor — who was the only candidate on the ballot.

Garodnick was given a long and rousing standing ovation and earned a host of subsequent acclaim for not drawing out the months-long fight.

“It is not lost on me that this became a tense and grueling process for many of us,” said Garodnick, whose family was also in attendance. “Please know that I will do my part to resolve any rifts that this process may have caused among us, and am here to take any steps necessary to help us move forward together.”

Still, almost immediately he and supporter Annabel Palma (D-Bronx) put Mark-Viverito on notice that they wouldn’t be sidelined.

After Mark-Viverito named only her supporters to serve as members of the powerful Rules Committee, which will help choose committee chairs over the next two weeks, the two objected. The new speaker said later that others would be added to the Rules panel down the road.

After the meeting, Mark-Viverito said she intended to remain independent from de Blasio and to hold his administration accountable, even though it was the mayor’s lobbying that was instrumental in getting her the job.

Sources said de Blasio pressured councilmembers and cut a deal with Brooklyn Democratic boss Frank Seddio to get him to break from an alliance with Queens and Bronx county leaders who backed Garodnick.

In exchange, Seddio was promised his members would get plum chairmanships and that he could play a significant role in filling lower-level council posts, according to sources.

De Blasio praised his longtime ally’s big win.

“I think she’s going to be a great partner in government,” said the mayor.

“There’ll be times that we disagree, and I know she’ll fight energetically for what she feels is right.”

When asked to name her priorities, Mark-Viverito declined to give specifics — saying the council’s agenda would be determined collectively.

But she did make it clear there would be changes ahead.

“There has been definitely a sentiment expressed by a majority that we want to head in a different direction with regards to how we operate as an institution,” she said.

One early example of the change — and of the diversity of the Council — was the number of members who made statements in a mix of Spanish and English, or even only in Spanish.

Mark-Viverito herself used both languages in her speech and kept bouncing between the two depending on the language questions were lobbed.

But after she said in Spanish she hadn’t known the outcome of the voting before arriving at City Hall, she declined to repeat the answer in English.