LOS ANGELES  New Zealand will remain the middle of Middle-earth, at a price.

After meeting in Wellington with visiting Hollywood studio executives, New Zealand officials, led by the prime minister, agreed to an extraordinary deal on Wednesday under which they will contribute special financing and introduce new labor legislation to keep the filming of Warner Brothers’ two movies based on J. R. R. Tolkien’s “The Hobbit” on their shores.

It may seem that New Zealand has grown sentimental over its Hobbits  or rather the filmmaker Peter Jackson’s Hobbits. It has built a tourism industry and no small amount of national pride around the creatures, dating to when Mr. Jackson’s “Lord of the Rings” franchise established a growing movie industry sometimes called Wellywood there.

But behind the agreement is a hard economic reality. Hollywood has the upper hand in deciding where to film its big-budget extravaganzas, and there are many places willing to pay to attract filmmakers. And so the negotiations found executives of a giant American studio sitting across the table from the chief executive of a sovereign nation, population 4.4 million, wrangling over the fate of a pair of films and, with it, a not insignificant part of that nation’s economy and public image.

Prime Minister John Key announced the agreement at a news conference late Wednesday in Wellington, after a week in which thousands of film workers had taken to the streets in support of efforts to save the movies after a labor dispute had stalled production.