Lech Walesa and a government minister Wednesday concluded a historic pact to legalize the independent Solidarity trade union after a seven-year ban and to hold Poland's first free elections since World War II.

The Solidarity leader, representing the opposition, and the interior minister, Gen. Czeszlaw Kiszczak, endorsed hundreds of pages of agreements hammered out during intensive talks on Poland's political and economic reforms.They approved the pact as participants in the 2-month-old negotiations gathered for a last time at the Council of Ministers Palace.

Earlier Wednesday, Walesa said Solidarity had achieved its primary goals.

"This is settled. Before Poland and all the world I am saying that we have achieved what we promised to do," he said.

Walesa spoke after a meeting of the Solidarity Executive Commission in which it agreed to approve the sweeping agreement with the government.

Asked when he thought Solidarity would be legalized, Walesa replied, "I think immediately after signing."

The government agreed in August to hold the talks in return for an end to a series of crippling strikes.

The negotiators agreed to amend the present trade union law to allow legalization of Solidarity, registration of farmers' Rural Solidarity and the reinstatement of workers fired for Solidarity activities after the December 1981 crackdown that quashed the trade union movement.

On Tuesday, negotiators agreed to a plan that would bring the opposition into the Sejm, the one-house parliament, as a minority party. Under the plan, a freely elected Senate and a powerful presidency would be established.

Walesa said the union did not get everything it wanted in the agreement but said other issues could be addressed later, "when we are a legal union."

Solidarity became the first legal, independent union movement in the Soviet bloc when it was chartered in the fall of 1980 during nationwide strikes. It was suspended in the December 1981 military crackdown and later outlawed.

The present one-house parliament could meet as early as Friday to enact the accords. Under the agreement, the opposition would join in elections in June that would guarantee the Communist Party and its allies 65 percent of the Sejm's 460 seats. That ratio would apply only this year with later elections to be more democratic.

A presidency and a Senate of about 100 members would be created. The Senate would be elected freely and probably would be controlled by the opposition. The Senate would advise the Sejm and exercise veto powers over its bills.

The president would be elected this year by the Sejm and Senate for a six-year term.