Mayor Bill de Blasio thought his search for a schools chancellor was wrapping up.

After a lengthy search, city officials had identified a top contender in Miami-Dade County Superintendent Alberto Carvalho, a rising star in education circles who would have been a high-profile get for the mayor. Almost two weeks ago, Carvalho was offered the job leading the nation’s largest school district. A week ago, after many conversations and clandestine trips to the city, Carvalho signaled he would take it.

Then on Thursday, it all fell apart in dramatic fashion during a four-hour emergency school board meeting in Miami that was carried live on television. It all left New York City back at square one.    

“Like many of you, I was very surprised by Mr. Carvalho’s decision,” de Blasio said at a packed news conference Thursday afternoon, at which he described the timeline of the city’s discussions with Carvalho. “I thought we had found the right candidate.”

The surprise turn of events left education observers in both cities wondering what happened in the time between Carvalho apparently confirming that he would move to New York City and his statement that stunned the mayor and his top officials.

Here’s what we know so far about how the events played out across two cities.

The city’s courtship of Carvalho involved multiple meetings with the mayor at Gracie Mansion — one in January and another in February, according to mayoral spokesman Eric Phillips.

In Miami, rumors began swirling on Tuesday that Carvalho had been offered the post. School board member Martin Karp met with the superintendent the next day on the seventh floor of the school board building in downtown Miami.

Karp says he didn’t ask whether Carvalho had already made up his mind — perhaps the long-serving school board member didn’t want to hear the answer. Instead, he made a final case for Miami, and walked away thinking his city still had a chance to keep its longtime superintendent.

“I felt that he’s vested around 30 years in this community, that he has a lot of ties here, that he has established a tremendous amount of relationships,” Karp said. “You work very hard to establish those things.”

By Wednesday afternoon, news that the search had narrowed to Carvalho had spread beyond South Florida — and reached the rest of Miami’s school board members, who called an emergency meeting just after 4:30 p.m.

“There was an unusual situation in terms of some news starting to move around,” de Blasio said at the press conference Thursday. “His school board as a result called this meeting.”

The media was indeed onto the story. At 4:52 p.m., Chalkbeat informed City Hall of plans to immediately run a story reporting that the search had narrowed to Carvalho.

The city already had reached an agreement to share the news with Politico that Carvahlo was the city’s choice, and de Blasio said that at about 5 p.m, Carvalho “confirmed that he was very comfortable giving the information to Politico.”

Politico broke the news at 5:20 p.m. Immediately after, City Hall officials began confirming to the other reporters and the public that Carvalho was the pick.

Carvalho was quickly overwhelmed with messages of support and pleas for him to stay. The superintendent said 500 texts clogged his inbox, along with as many missed phone calls.

In lower Manhattan, City Hall scrambled to notify members of the Panel for Educational Policy, the school-oversight board which had been scheduled to vote Wednesday evening on the largest round of proposed school closures since de Blasio took office.

Panel members were hastily assembled at 5:30 p.m. and placed on a conference call with City Hall officials, who explained that Carvalho would be the next chancellor. Some of the members expressed frustration that they had only been brought in the loop after the news had been publicly reported.

“Panel members were very upset by this whole thing,” said one person who was in the room.

By 6:30 p.m., Carvalho’s chief spokeswoman released a cryptic statement: “The superintendent has been offered the job, but has not yet accepted,” wrote Daisy Gonzalez-Diego. (That was the start of a back-and-forth over the nature of Carvalho’s communication with de Blasio that would last into Thursday, with Gonzalez-Diego telling Miami public radio affiliate WLRN, “I don’t know that ‘accepted’ the job was the right word.”)

Despite the breaking news, the Panel for Educational Policy members were scheduled to vote on a series of 13 school closures. As they prepared to start what would become one of the most contentious panel meetings in recent history, Chancellor Carmen Fariña told at least one panel member that the city would hold a press conference the next morning to officially unveil their new pick. She noted that the monthly panel meeting would be her last as chancellor.

“She was very positive about the selection,” said one panel member who spoke with her about Carvalho.

The meeting eventually drew so many angry lawmakers and parents that some panel members wondered whether the timing of the city’s announcement of Carvalho was meant to crowd out coverage of the closures. “The thought crossed my mind,” said one panel member, who spoke on condition of anonymity.

Meanwhile, back in Miami, education insiders were also pulling a late night — and many thought they were hours away from seeing their schools chief walk away.

Miami community activist Tangela Sears said she stayed up speaking with the superintendent and bombarding his phone with texts.

At that point, “I think Alberto was heading to New York,” said Sears, an outspoken advocate against gun violence in Miami, a cause for which Carvalho has been an outspoken ally. “I think he was battling the decision, but I think he was heading to New York.”

First thing Thursday morning, Carvalho was flanked by television cameras at iPreparatory Academy, the downtown Miami school where he serves as principal. He declined to say anything about New York City, instead railing against the idea of arming Florida teachers.

When the Miami-Dade school board came together at 10 a.m. Thursday, desperate Miamians lavished the superintendent with praise and begged him to stay put. The show of support dragged on for hours while Carvalho sat silently, at times tearing up, touching his heart, and wrapping one student up in a hug on the dais. School board members passed a symbolic, and unanimous, vote of confidence in the superintendent.

It took four hours, two breaks, and a soaring speech before Carvalho revealed his final decision.

“I just don’t know how to break a promise to a child, how to break a promise to a community. And that has weighed on me in the past 24 hours,” Carvalho said. He would stay.

To observers in Miami, the outpouring of support is what finally pushed Carvalho to call de Blasio during the meeting, with a surprising message: thanks, but no thanks.

“If he had a commitment, either way, I think at that moment he felt something inside. ‘This is what I have to do for these kids,’” said school board member Larry Feldman.

Monica Disare contributed reporting.