The Pentagon’s inspector general has cleared the top U.S. commander in Afghanistan of wrongdoing following an investigation into whether he exchanged inappropriate e-mails with the same Tampa socialite involved in the scandal that prompted David H. Petraeus to resign as CIA director, U.S. officials said Tuesday.

The FBI uncovered messages from Marine Gen. John R. Allen during its investigation of Petraeus last year. The tenor of some of the e-mails, which senior defense officials described as racy and flirtatious, prompted Defense Secretary Leon E. Panetta to order a formal inquiry.

In a letter sent to Allen on Friday, the inspector general wrote that Allen had not violated military prohibitions against conduct unbecoming an officer, according to the senior U.S. officials, who spoke on the condition of anonymity because they were not authorized to discuss the matter on the record. “He was completely exonerated,” one of the officials said.

Panetta was informed that the investigation had cleared Allen, according to Pentagon spokesman George Little. “The secretary has complete confidence in the continued leadership of General Allen, who is serving with distinction in Afghanistan,” Little said.

Allen exchanged the messages with Jill Kelley, 37, who ingratiated herself with several senior officers at the Tampa headquarters of the U.S. Central Command. Kelley’s complaint to the FBI about another set of messages — ones that were harassing — eventually led to the discovery of an affair between Petraeus and his biographer, Paula Broadwell. The FBI determined that Broadwell, for reasons still not clear, had sent Kelley the harassing e-mails.

A spokesman for Allen declined to comment.

The inspector general’s investigation prompted the White House to place on hold Allen’s nomination to become the supreme allied commander in Europe. Allen is scheduled to relinquish command in Afghanistan early next month, and the Pentagon has not yet requested that the Senate Armed Services Committee reschedule his nomination.

Defense officials have said Panetta’s decision to refer the e-mails to the inspector was driven by the content of some of the messages and by a desire to show that the Pentagon was not trying to ignore any potential miscount in the wake of the Petraeus scandal.

Although the messages have not been released, some military officials sympathetic to Allen questioned whether Panetta overreacted, placing a cloud over the general’s head at a critical juncture in the Afghan war. A senior defense official said Panetta referred the matter to the inspector general upon the recommendation of civilian and military attorneys.

Allen has spent the past few weeks refining his recommendations for the number of U.S. troops that should be withdrawn from Afghanistan this year and the number of forces that should be stationed in the country once the U.S. and NATO combat mission ends in 2014.

Senior military and administration officials expect Allen’s preferred options, which have not yet been formally submitted to the Pentagon, to entail more troops than those favored by top civilian aides to President Obama. Allen wants to keep about 9,000 U.S. troops in Afghanistan after 2014, while White House officials are leaning toward a force of 2,500 to 6,000.

Although initial reports described the volume of messages between Allen and Kelley as up to 30,000 printed pages, the two exchanged only a few hundred messages over a multi-year period, one of the U.S. officials said. “Some of the messages are not the sort of things you would print in a family newspaper,” the official said. “But that doesn’t mean he violated military regulations by sending and receiving them.”

Officials close to Allen have long insisted that he did not have a sexual relationship with Kelley. Allen’s partisans said that Kelley was a close friend to Allen and his wife, Kathy.

Many of the messages related to social events or to items Kelley had seen in the news, said a senior official close to Allen. Sometimes she wrote to compliment the general on a television interview, the official said, and sometimes she copied him on a message intended for his wife. “He returns almost every e-mail,” the official said soon after the investigation commenced. “To him, it’s a sign of politeness.”

The disclosure that Allen exchanged potentially inappropriate messages with Kelley prompted disbelief among many officers who worked with him. The 6-foot-tall, silver-haired Allen has the demeanor of an avuncular professor, not a hard-bitten Marine prone to flirting and carousing.

Allen, a native of Warrenton, Va., was the first Marine to serve as the commandant of midshipmen at the Naval Academy. He spent two years in Iraq’s Anbar province, where he led an effort to reach out to Sunni tribal leaders to try to persuade them to stand against al-Qaeda militants — a shift that helped turn the course of the war in western Iraq.

He eventually became Petraeus’s deputy at the Central Command, where his portfolio focused largely on Iran. The job afforded him the opportunity to brief Obama, who grew impressed by the general’s analyses. When Obama appointed Petraeus to head the CIA, Allen was tapped to go to Kabul in 2011.