The New Zealand government said Wednesday that “The Hobbit” would keep its feet firmly planted in the small South Pacific nation, ending a short but intense saga over where the expensive two-picture production would be filmed and averting a fallout that would have damaged all sides.

After two days of talks with Hollywood executives, New Zealand Prime Minister John Key announced that his government had reached a deal with Warner Bros. to shoot the prequel to the “Lord of the Rings” films in the country.

Warner Bros. and filmmaker Peter Jackson had threatened to change locations for the planned February production of the $500 million-budget project after a labor dispute that erupted between Jackson and the New Zealand Actors Equity, which was seeking to provide union benefits to actors on “The Hobbit.”

“I am delighted we have achieved this result,” Key said in a statement. “Making the two Hobbit movies here will not only safeguard work for thousands of New Zealanders, but it will also follow the success of the ‘Lord of the Rings’ trilogy in once again promoting New Zealand on the world stage.”

The agreement comes after Key met with New Line Cinema President Toby Emmerich and Warner Bros. home entertainment chief Kevin Tsujihara in an effort to persuade the studios to keep the production from exiting New Zealand.

The Hollywood executives wanted assurances that Warner Bros.’ investment would not be threatened by future labor unrest and appeared to be using the prospect of shifting locations as leverage to gain more concessions from the New Zealand government.

They succeeded: As part of the agreement, the government said it would broaden the criteria for its film fund, entitling the producers to a rebate of as much as $7.5 million for each of the “Hobbit” movies beyond the $60 million to $75 million subsidy that producers would be eligible to receive under New Zealand’s film rebate program.

The government also said it would offset $10 million in Warner Bros.’ marketing costs as part of a “strategic partnership” with the studio to “promote New Zealand as both a film production and tourism destination.”

Additionally, Key said the government would introduce legislation clarifying how workers are classified in the film industry. In 2005, a model maker for Jackson’s design studio Weta Workshop successfully sued to be classified as an employee rather than a contractor, who is paid less and has fewer workplace protections. The case has opened the door to similar claims.

“We will be moving to ensure that New Zealand law in this area is settled to give film producers like Warner Bros. the confidence they need to produce movies in this country,” Key said.

Such a move, however, is likely to anger workers who are seeking union benefits and was sharply criticized Wednesday by a top union leader in New Zealand.

Studio representatives declined to comment.

Warner Bros., its New Line Cinema unit and co-financing partner Metro-Goldwyn-Mayer recently greenlighted production of “The Hobbit,” which has faced years of delays.

The labor dispute caused deep anxiety in New Zealand, where the film and television industry is one of the largest private employers and pumps hundreds of millions of dollars into the economy annually. This week, more than 2,000 actors, crew members and technicians marched in Wellington, the capital, to support keeping the project in New Zealand.

For Warner Bros., New Line Cinema and MGM, moving production just months before filming was set to start would have been costly, given that the studios have spent tens of millions of dollars over the last two years preparing to shoot the movies in New Zealand.

Furthermore, the studio might have faced a public relations backlash and incurred the wrath of unions such as the Screen Actors Guild, which had recently lifted a boycott of the planned production after a producers group in New Zealand gave assurances that it would hold talks with New Zealand Actors Equity.

For Jackson, a native New Zealander who has been instrumental in building the country’s film industry, leaving his home turf would have alienated many of his countrymen.

And the New Zealand government was highly motivated to do whatever it could to keep “The Hobbit” on its shores, fearing that losing such a high-profile movie from one of the world’s biggest directors would tarnish the country’s image as a friendly mecca for global movie making.