After months in which Greece teetered on the verge of bankruptcy, European officials agreed Tuesday to give the country a second massive bailout in exchange for harsh austerity measures, as grim new estimates about the country’s economy pushed off a resolution until what some officials called the last possible day to reach one.

The decision buys time for the Mediterranean country to try to fix its staggering problems, and gives assurances to the world that a Greek default — and its possibly disastrous ripple effects — will be forestalled, at least for now. If Greece had been cut loose, it would have defaulted in late March, and doubts about the viability of larger countries such as Spain and Italy might have grown.

Under the terms of the deal, private bondholders will take a larger loss than had previously been planned in an attempt to get Greece’s debt to what European officials consider a sustainable level by 2020. The officials also agreed to reduce the interest they charge Greece for the long-term loans.

“We have reached a far-reaching agreement on Greece’s new program and private-sector involvement that will lead to a very significant debt reduction for Greece,” Luxembourg Prime Minister Jean-Claude Juncker, who heads the bloc of 17 countries that use the euro, told reporters in Brussels after a 14-hour negotiation. He called the amount of aid “unprecedented.”

But a recent European assessment about Greece’s finances left questions about whether the country would truly be able to live up to the stringent demands to which it has agreed, or whether it would default eventually even with a second bailout. Reflecting that uncertainty, as well as concerns over the hurdles Greece must clear before it secures the aid, European markets were uniformly down in midday trading on Tuesday. Germany’s DAX was 0.82 percent lower; Britain’s FTSE 100 fell 0.42 percent, and the euro was flat against the dollar.

Greek officials said Tuesday that the deal would help put their country back on a sustainable track.

“We now have the ability to progress with stability, to limit uncertainty and to increase trust in the Greek economy in order to create better conditions,” Greek Prime Minister Lucas Papademos was quoted as saying in the Kathimerini newspaper.

Under the plans negotiated between the Greek government and European leaders, officials said Tuesday, Greece will receive a $172 billion bailout in exchange for taking harsh austerity measures that officials believe will make it more competitive. Greece will also write down 53.5 percent of the nominal face value of its privately held debt as a way of getting back on track by 2020.

The aid would come atop a previous bailout of $145 billion in May 2010, making Greece, a country of 11 million people that is just 2 percent of the euro zone’s economy, the recipient of by far the largest bailout in European Union history.

Last week, Juncker vowed to spend his weekend doing everything he could to find ways to further cut Greece’s debt. But by Monday, it became clear just how short it would fall of its targets.

An internal public-creditor document showed that Greece would have a debt by 2020 of 129 percent of its gross domestic product, Reuters reported, well short of the 120 percent target that they believe would keep Greece from going bankrupt in the future.

Even small failures to meet interim targets along the way could send that ratio shooting higher, the report said. The deal reached Tuesday morning would help Greece get closer to its goal, bringing the debt down to 120.5 percent in eight years. But the same threats — of an exacerbated recession, of political difficulties implementing the cuts — remained.

Some European countries — Germany and the Netherlands chief among them — drew a hard line on giving Greece any more assistance if no plan was devised that got the country closer to the 120 percent goal.

“We can help, but we are not going to pour money into a bottomless pit,” German Finance Minister Wolfgang Schaeuble told German radio last week, jarring Greeks, who raised the specter of the Nazi occupation of their country.

Without the aid, Greece would be bankrupted when a $19 billion debt payment comes due on March 20. But because of the complexity of the deal, its outlines must be set far earlier.

Greece will have to allow a roughly two-week-long window for its bondholders to sign on to the plan to write down debt, and parliaments in Germany, the Netherlands and Finland will have to approve their countries’ contributions to the aid.

“It cannot wait any longer,” French Finance Minister Francois Baroin said Monday in an interview with Europe 1 radio.

But many Greeks question whether the terms of the bailout will do much to help their economy in the coming years, and the “troika” of the International Monetary Fund, European Union and European Central Bank acknowledged that the recession would worsen in the short run, even as unemployment has already spiked to 21 percent — 49 percent for those younger than 25 — and the economy contracted by 7 percent in the third quarter of 2011.

Europe has demanded that the public sector shrink by 150,000 people, that the minimum wage be lowered by 22 percent, that pensions be cut and that Greece do more to sell off its publicly owned companies, among other measures that filled a 50-page booklet.

When the Greek parliament started implementing them last week, 43 of the deputies in the ruling coalition rebelled, and rioters in Athens set dozens of buildings on fire.

European leaders, stung by the sense that Greek leaders failed to live up to the promises they made to qualify for the first bailout, have said they would channel new bailout funds into a special escrow account that Greece will be able to use only to make debt payments, not for its ordinary government expenses, in essence forcing Greece to prioritize its creditors over its own citizens. And they said they would increase their monitors in Athens to make sure Greece was meeting targets.

“I am in favor of more control, more supervision,” Dutch Finance Minister Jan Kees de Jager told reporters in Brussels on his way into the meeting. “Money is the thing we can control Greece with.”

Few expected this to be the last time officials gather in Brussels to discuss the country’s fate, even assuming the bailout proceeds without hiccups.

If Greece fails to take the difficult measures that are its end of the deal, Europe may be inclined to cut off the money, especially after a permanent bailout fund is set up in July that would do more to keep the pain of a Greek default isolated inside the country.

“The message that was sent by policymakers, especially in Germany, was that the price for keeping Greece in the euro zone is not infinite,” said Jean Pisani-Ferry, the director of Bruegel, an economic think tank in Brussels.