He fought with distinction, historians say, in two of this country’s formative wars. He was given a medal for valor by one of the world’s great generals. He met a president and at least one president-to-be.

Yet James Robinson, who was born into slavery in Maryland in the mid-18th century, was denied his liberty for most of his life, and he never received the military honors he’d earned.

That changed over the weekend.

Robinson, an Eastern Shore native whose 1868 obituary described him as “loved by all and venerated by all,” was given a military funeral Saturday in his adopted hometown of Detroit.

Sponsored by two military legacy organizations, the event at the historic Elmwood Cemetery included an honor guard, a flag presentation, speeches, a rifle volley and the dedication of two bronze emblems representing the conflicts in which he fought: the Revolutionary War and the War of 1812.

A handful of surviving references to Robinson point to him as a hero’s hero: the Marquis de Lafayette pinned a gold French military medal of honor on him for his exploits at the Battle of Yorktown in 1781, and he was in the thick of the combat that helped Gen. Andrew Jackson rout the British at the Battle of New Orleans in 1815.

But as one of the more than 5,000 black people who fought in the Revolutionary War, and several thousand who took up arms for the United States in the War of 1812, his deeds were mostly lost to history, as records on African American soldiers were spottily kept.

Worse, he fought in both wars on the understanding that afterward, he’d be given the opportunity to live as a free American citizen. Instead, he returned home each time only to be sent back into the “most grievous bitter bondage” — slavery — in which he spent at least 77 of his 115 years.

One historian who helped unearth Robinson’s story says it epitomizes the plight of thousands of black troops who fought for the United States in two centuries’ worth of wars, only to return to a land that denied them the freedoms they had secured for the nation.

“What more can you do to serve your country and to secure your rights, to secure equal citizenship, than that man did?” asks Maurice Barboza, the founding director of the National Mall Liberty Fund D.C., a nonprofit that aims to build a monument in Washington to black veterans of the Revolutionary War.

It was two years ago that Barboza met Elijah Shalis, an official with the Michigan Society of the Sons of the American Revolution and the organizer of Saturday’s event.

The two have drawn on sources — census reports, news items, a Boys’ Life magazine article, a history of Elmwood, even a newly rediscovered 64-page memoir Robinson narrated in 1858 — to construct a portrait of a man who fought bravely, who became embittered at the treatment he received, and who retained a craving for freedom throughout an incredibly long life.

Details about his Maryland origins are thin. His narrative says he was owned by a man named Francis de Shields — or Francis Shiel, according to a different source — a colonel in Gen. George Washington’s Continental Army who brought Robinson into the service with him.

Owen Lourie, a historian with the Maryland State Archives, said he could find no mention of Robinson or de Shields in the archives, but the enlistment scenario was plausible.

“We know that soldiers brought their slaves with them, and a well-off gentleman would never be seen without his body servant,” Lourie said.

A private in a Maryland light infantry regiment, Robinson would have been one of about 755 black soldiers — and 95 black Marylanders — historians say served in the Continental Army.

His narrative — authored with a ghost writer and under the name James Roberts — describes Robinson scalping Indians and taking part in skirmishes on the Eastern Shore. Other accounts suggest he fought at the Battle of Brandywine in 1777 and at Yorktown, where the British surrendered in 1781.

At Yorktown, he’s said to have charged up a British rampart and killed three men in hand-to-hand combat en route to overtaking the emplacement. The victorious allied leader, Lafayette, pinned on Robinson a gold medal of valor by virtue of his authority as a French nobleman.

“That was extraordinary, because very few medals were given for service in the Revolutionary War,” Shalis said, adding that the moment showed “the French, even then, were more tolerant of minorities.”

De Shields had promised he would free Robinson after the war, but he died soon afterward.

His heirs sold the married war veteran to Calvin Smith, whose plantation was in Louisiana or Mississippi. Robinson described Smith’s place as “a slaughter-house of human beings” and recalled being whipped so badly he “could not keep the vermin out of my flesh for weeks at a time.”

“I will now confess that, could I have foreseen what heart-sickening ills awaited me in the future, I should have been strongly tempted to make my way to Canada,” he added.

Robinson was still Smith’s property in 1813 when Jackson swept through the area to enlist men of every background — slaves, free black men, privateers, Choctaw Indians — in advance of the British attack on New Orleans.

In Robinson’s words, “Jackson came into the field, chose out the ones he wanted, and then addressed us thus: ‘Had you not as soon go into the battle and fight, as to stay here in the cotton-field, dying and never die? If you will go, and the battle is fought and the victory gained on Israel’s side, you shall be free.’ This short speech seemed to us like divine revelation, and it filled our souls with buoyant expectations.”

Robinson, then 61, went into battle, and he describes losing his left index finger — and reacting by “taking the heads off” six redcoats — as part of a victory in which “sixty or seventy or more of the colored men” were killed.

Afterward, he recalls, he requested his reward from Jackson.

“’Before a slave of mine should go free, I would put him in a barn and burn him alive,’” he quoted the future president as saying before returning Robinson to Smith.

Robinson somehow obtained his freedom in the 1830s; the 1840 Census lists him as a free man in Ohio. He later became a Methodist minister and married a woman named Curtilda. They had two sons, one of whom fought for the Union in the Civil War.

His last known descendant, a granddaughter named Gertrude Robinson, died in Ohio in 1983.

Robinson’s memoir also has him traveling to Washington in 1856, at 103, seeking a military pension. According to the narrative, he met with President Franklin Pierce.

“He told me that I was nothing but goods and chattels, like a horse or a sheep,” Robinson wrote, “that my master had got the pension, and was still receiving it, or his heirs. He said it would be a disgrace to take it from the white man and give it to the negro . . . ‘When you fought that battle, you was your master’s property.’ ”

Lourie advises that the narratives of former slaves, while valuable sources, “need to be read cautiously,” as the accounts often come filtered through those who arranged for their publication, usually abolitionists with “their own points to make.”

But to those who might find it hard to believe that a former slave could have met such influential figures, Barboza and Shalis say it’s likely that Robinson did, in fact, know Lafayette — one Robinson obituary says the pair met again in 1824 — and that would have opened doors for the veteran.

Shalis assembled much of the timeline of Robinson’s life as part of confirming his eligibility for membership in the Sons of the American Revolution and the Michigan Society of the War of 1812, the groups that sponsored Saturday’s ceremony.

Doing that research was a bittersweet experience, he says, in that a great American came into view, one who never lost his determination to be free, no matter how cruelly the country he served treated him for most of his life.

An earlier version of this story incorrectly stated that the military funeral for James Robinson featured a 21-gun salute. The ceremony featured a rifle volley.

— Baltimore Sun