State television broadcast footage of Assad standing to pray in a large mosque in Hama behind its imam, with other clerics standing alongside and a large crowd of worshippers.
State news agency SANA quoted the preacher as saying that Assad's presence in the city for Eid showed that victory and the return of security were only "a few steps" away.
Syria's civil war has turned to Assad's favour since 2015, when Russia sent its jets to help his army and allied Shi'ite militias backed by Iran turn back rebels and win new ground.
Since the war began in 2011, it has killed hundreds of thousands, driven millions more from their homes, sparked a global refugee crisis and drawn in regional and world powers.
The conflict is far from over. Rebels hold swathes of the country, including around Idlib province near Hama, and launched a new attack in Quneitra in the southwest on Saturday.
Rebels also hold the Eastern Ghouta area near Damascus, parts of the desert in the southeast and a large pocket south of Hama around the city of Rastan.
As recently as March, rebels advanced from Idlib province to within a few kilometres of Hama, before the army and its allies pushed them back in weeks of fierce fighting.
However, the army drove insurgents from their biggest urban stronghold in Aleppo in December and have also forced several important rebel enclaves to surrender over the past year.
FOCUS ON ISLAMIC STATE
Assad has not made a declared visit to Hama, which is about 185 km (115 miles) from Damascus, since the war began. Last year he delivered Eid prayers in Homs, about 40km (25 miles) closer to Damascus.
Early in the crisis he visited Raqqa, a city that has since become the Syrian capital of Islamic State and now faces an assault by a US-backed coalition to drive out the militants.
The fight against Islamic State, which has attacked Western cities, has become the focus of Western leaders, some of whom have softened demands that Assad must quit to end the crisis.
In March, US Secretary of State Rex Tillerson said Assad's fate would be decided by Syrians, a change in rhetoric after years of insisting he step down to allow a political solution.
France's new President Emmanuel Macron said this month he did not see Assad's departure as a condition to end the fighting and the priority was stopping Syria becoming a failed state.
The US and other Western states, along with Turkey and Gulf monarchies, have long supported some of the rebels, an array of groups that includes Islamist and nationalist factions. Assad describes them all as terrorists.
His military has said its focus is on the campaign in the desert, where it is advancing against Islamic State to relieve a besieged government enclave in the city of Deir al-Zor.