Prime Minister Menachem Begin ended his official visit here yesterday with the United States and Israel agreeing to a strengthened security relationship that will involve the two countries in unprecedented new areas of formal military cooperation, including possible joint exercises and the stockpiling of U.S. equipment in Israel.

The agreement, which had been sought eagerly by Israel, represented a major triumph for Begin, whose relations with the Reagan administration had been strained severely by his recent provocative air strikes against Iraq and Lebanon and by his continuing open opposition to the administration's plan to sell $8.5 billion worth of jet fighter enhancements and sophisticated radar planes to Saudi Arabia.

Secretary of State Alexander M. Haig Jr., asked about the U.S. anger over the air strikes that had prompted President Reagan to hold up the shipment of American-made F16 jet fighter-bombers to Israel temporarily, replied:

"We viewed these as events that are now behind us. We regarded them as generated by events that also are now behind us."

Instead, Haig said at a news conference, the administration took the position, in two days of talks with Begin, that its emphasis should be to "put some meat on the bones" of its hopes for a "strategic consensus" involving Israel and other friendly Mideast nations in joint planning against "external threats to the region" by the Soviet Union, its proxies or terrorists.

In describing the accord, Haig, clearly sensitive to the suspicion and controversy it is likely to provoke in the Arab world, stressed repeatedly that the specific forms of cooperation are still in a very preliminary planning stage and, in the short term at least, will be subject to several "practical and political" constraints.

Haig was very careful, for example, to say that what he called "limited, initial pre-stocking" of equipment in Israel probably will be confined to medical supplies. He also hinted that any joint maneuvers are likely to involve naval forces at sea rather than troop exercises on Israeli terrain.

In addition, he pointed out several times that Begin and his delegation had come to Washington "with a much more fulsome list" of proposed cooperative military ventures.

However, Haig said, administration officials had made clear that budgetary restraints in this country, long-range U.S. defense-planning priorities and the U.S. requirement for good relations with the Arab world require that increased military cooperation with Israel be kept on a modest scale for the time being.

Still, Haig left no doubt of the administration's belief that Begin shares its concern about the threat of Soviet expansionism in the Middle East and that the time has come to translate that shared concern into concrete forms of cooperation.

Haig said the United States is not opposed to codifying the agreement in a written memorandum of understanding, and added, "I suppose we will try to draft one." Such a written agreement, he said, would serve as "the launching pad" for implementing cooperative measures.

Detailed discussions on how to implement the agreement are to begin today between Defense Secretary Caspar W. Weinberger and Israeli Defense Minister Ariel Sharon. Haig said they will continue at a high level under the general direction of the Defense Department in the weeks ahead.

Haig's view that the visit, which marked the first meeting between Begin and Reagan, had been "exceptionally successful" was shared by Begin. At a separate news conference earlier, the prime minister hailed the accord as the opening of a new chapter in U.S.-Israeli relations, and said the two countries have "a clear common interest in living in permanent alliance."

Begin denied that U.S. agreement to stronger military ties was a form of compensation to Israel for the Saudi arms package or a reward for the restraint he has shown in his criticisms of the sale while on Reagan's home ground.

Israel, he said, still regards the sale of sophisticated weaponry to the Saudis as a threat to Israeli security. But, he added, Israel can defend itself, and he said his government wanted the strategic cooperation agreement not as protection against its Arab adversaries but to help bar the Middle East to penetration by the Soviet Union.

Referring to another high-priority issue on the Mideast agenda, the deadlocked negotiations with Egypt on self-government for the Palestinian inhabitants of Israeli-occupied territories, Begin said he hoped his recent agreement with Egyptian President Anwar Sadat to resume the talks later this month will lead to an agreement by the end of the year.