An Algerian identified as Nou Mediouni was arrested in Zaragoza, northern Spain, the interior ministry said.
The other suspect, a Moroccan named Hassan El Jaaouani, was arrested in Murcia in the south.
They have “a similar profile to the two suspects who carried out the Boston attacks”, the ministry said. French police had helped to track them down.
Al-Qaeda in the Islamic Maghreb (AQIM) is among several Islamist groups fighting French and West African troops in Mali.
It has its roots in the bitter Algerian civil war of the early 1990s, but in 2007 it emerged as an international jihadist group linked to Osama Bin Laden’s network. AQIM says its aim is to spread Islamic law and it is known for kidnapping Westerners and extracting ransoms.
It is not clear whether the pair arrested in Spain are suspected of planning any attacks. Their similarity to the Boston marathon bombers was not explained in the ministry statement.
Moroccan Islamists linked to al-Qaeda were found guilty of the 2004 Madrid train bombings which killed nearly 200 people and injured more than 1,800.