BOGOTÁ, Colombia—Venezuela’s opposition came tantalizingly close to removing President Nicolás Maduro from power this week, according to more than a dozen people involved in talks to oust him.

But in the end, it all went wrong.

For the past two months, key opposition figures, led by Juan Guaidó, the young head of the National Assembly, had met with highly placed figures in Mr. Maduro’s government, including military officers accused of rights abuses, trying to cut a deal for a peaceful transfer of power.

Meetings took place in Panama and the Dominican Republic, as well as here in Colombia’s capital, according to interviews with more than a dozen people with knowledge of the talks.

While both sides were far apart at first, they came to agree on one thing: Mr. Maduro had to go.