We use this nowadays to mean in a state of worry or anxiety, but the literal meaning alludes to the “mourners’ bench,” or seat especially assigned to those of a congregation who, affected by the exhortations of a preacher, have become anxious over their future state and seek repentance.
The literal phrase was in use early in the nineteenth century, but the oldest record of the figurative use is in Harriet Beecher Stowe’s The Pearl of Orr’s Island, published in 1852.