Using a bayonetted rifle and a helmet to mark a grave.

When and where did this originate?

It’s called the Battlefield Cross, and probably originated during the American Civil War.

Couple of articles hereand here.

I wasn’t aware that soldiers in the Civil War wore military helmets.

No but they did have hats.

Is it not dumb to leave rifles behind on a battlefield?

They are just temporary markers. Originally it seems they were just used to mark where a solider fell so that the body could be retrieved and buried elsewhere. They then would have been removed.

From the first link, at present: