The spokesman, Bill Burton, said Mr. Obama’s broker bought the stocks without consulting the senator, under the terms of a blind trust that was being set up for the senator at that time but was not finalized until several months after the investments were made.

“He went about this process to avoid an actual or apparent conflict of interest, and he had no knowledge of the stocks he owned,” Mr. Burton said. “And when he realized that he didn’t have the level of blindness that he expected, he moved to terminate the trust.”

Mr. Obama has made ethics a signature issue, and his quest for the presidency has benefited from the perception that he is unlike politicians who blend public and private interests. There is no evidence that any of his actions ended up benefiting either company during the roughly eight months that he owned the stocks.

Even so, the stock purchases raise questions about how he could unwittingly come to invest in two relatively obscure companies, whose backers happen to include generous contributors to his political committees. Among those donors was Jared Abbruzzese, a New York businessman now at the center of an F.B.I. inquiry into public corruption in Albany, who had also contributed to Swift Boat Veterans for Truth, a group that sought to undermine John Kerry’s Democratic presidential campaign in 2004.

Mr. Obama, who declined to be interviewed about the stock deals, has already had to contend with a controversy that arose out of his reliance on a major campaign contributor in Chicago to help him in a personal financial transaction. In that earlier case, he acknowledged last year that it had been a mistake to involve the contributor, a developer who has since been indicted in an unrelated political scandal, in deals related to the Obamas’ purchase of a home.