The letter to Prime Minister Stephen Harper was released Thursday by the Prime Minister's Office.
Obama writes that, on behalf of the American people, he expresses his sadness over what he describes as a "devastating loss of life."
He says he was heartened to learn of the help U.S. firefighters offered, just as Canadians in the past have stood by the U.S. in difficult times.
"Our assistance to one another in dealing with such accidents is a mark of our close friendship and partnership," he wrote.
"My government stands ready to offer any additional support Canada may need in the aftermath of this tragic accident."
Other foreign governments, the Pope and the Queen have offered similar condolences.
It's unclear when the Obama letter was sent. The PMO released the letter, undated, after news of it surfaced publicly Thursday. The mayor of Lac-Megantic told a news conference that Harper's office had relayed the contents to her, and she expressed her appreciation for the note.
The July 6 tragedy was triggered by the derailment of oil cargo near the Canada-U.S. border, on a train belonging to the Maine-based Montreal, Maine & Atlantic Railway.
Firefighters from Maine rushed across the border to assist their Canadian counterparts in fighting the blaze.
Text of a letter by U.S. President Barack Obama, released Thursday by Prime Minister Stephen Harper's office:
Dear Mr. Prime Minister:
On behalf of the American people, I write to express my sincere condolences for the terrible tragedy on July 6 at Lac-Megantic, Quebec. We are saddened by the devastating loss of life caused by the train derailment and resulting fire. Our prayers are with the families and loved ones of the victims.
I was heartened that U.S. firefighters were able to support their Canadian counterparts in fighting the blaze, just as Canada has stood by the United States during trying times. Our assistance to one another in dealing with such accidents is a mark of our close friendship and partnership. My government stands ready to offer any additional support Canada may need in the aftermath of this tragic accident.