Ugandan police have detained two presidential hopefuls for several hours, according to police and aides of the politicians.

Former prime minister Amama Mbabazi, who wants to challenge President Yoweri Museveni for the ruling party's candidacy ahead of 2016 elections, was detained while he was on his way to meet supporters at a town near Kenya's border. He was freed late in the evening, an aide said.

Andrew Felix Kawesi, Uganda Police operations commander, was shown on local television station NSB confirming the arrest to journalists in Njeru, 75km outside the capital Kampala.

"I am telling you ... I am the police and I have arrested him. He has to stick to what the law requires ... there's no going to Mbale. He's now under incarceration," Kawesi said.

Mbabazi was travelling to Mbale, a town near Uganda's border with Kenya, to do a consultative meeting with his supporters and promote his candidature.

In June police had banned Mbabazi from holding meetings to promote his challenge against Museveni, saying their party had not yet nominated a candidate.

Mbabazi had enjoyed a close relationship with Museveni, who has been Uganda's leader since 1986, but ties between the two became strained in the recent years, with Museveni accusing Mbabazi of taking decisions in the ruling party without consulting him. Mbabazi was sacked by Museveni in 2014.

Museveni has been endorsed by the ruling National Resistance Movement to represent it in next year's election.

Mbabazi announced candidacy

However, Mbabazi, a former close ally of the president, announced last month he would challenge the 70-year-old incumbent for the nomination, sparking criticism from the government, which pointed out that many of the failings of which Mbabazi accuses Museveni happened when he was the party's secretary-general.

Police also arrested another presidential hopeful, opposition leader Kizza Besigye, early on Thursday morning as he left his house, his aide said.

"He had just left his home this morning going to the American Embassy for a meeting with the ambassador when police intercepted him and ordered him either to return home or be arrested," Francis Mwijukye, his aide, told the Reuters news agency.

"He refused to return home and was immediately arrested and taken to Nagalama Police Post," he said, referring to a neighbourhood outside the capital Kampala.

Besigye was freed later in the day.

Police spokesman Fred Enanga told the state-run New Vision newspaper that Besigye and Mbabazi "recorded statements and had since been released unconditionally".

After he lost elections in 2011, Besigye led repeated anti-government protests, at which he was regularly arrested and held for a few hours before being released.

Following his rise to power following a guerrilla war, President Museveni said he would not rule like some African leaders who kept power for too long.

He has since fallen out with some of his former comrades, including opposition leader and three-time presidential challenger Besigye, who accuse Museveni of straying from the ideals that led them to wage a war against dictatorship.