Less than a week after Mattocks returned from leave in late March 1864, he was moved from the 17th Maine Infantry and placed in command of the 1st United States Sharpshooters. He commanded the 1st United States Sharpshooters in the Battle of the Wilderness, after which he was captured by the Confederate Army. He was a prisoner for six months, until he escaped the Confederate prison camp outside Columbia, South Carolina on November 3, 1864, alongside Captain Julius Litchfield and Lieutenant Charles Hunt, officers from other Maine regiments who were captured in the same battle. On their way back to Union territory, they trekked for two days without food before encountering a slave, who fed them "chicken, corn bread &amp; potatoes." He also received material support from both Union and Confederate Freemasons while in prison camps and during his escape. Mattocks was recaptured from the Union lines, on November 28, by Cherokee scouts, then in service of the Confederacy. He was forced to walk over to Morgantown, West Virginia, where they were then transported by train to a prison camp in Danville, Virginia. Mattocks was exchanged and released in late February, 1865. Rather than resuming his previous command of the 1st United States Sharpshooters, Mattocks was granted permission from the Secretary of War to rejoin the 17th Maine Infantry Regiment as major. Upon his return, he first served outside Petersburg, Virginia. After the fall of Petersburg, Mattocks led the regiment in the Appomattox Campaign and was distinguished in leading a charge at the Battle of Sailor's Creek. Sailor's Creek was the last battle of the American Civil War in which Mattocks participated. Three days later, on April 9, 1865, Confederate General Robert E. Lee surrendered the Army of Northern Virginia at Appomattox Court House.