Spain's new Prime Minister Jose Luis Rodriguez Zapatero has said Spain and Morocco will join forces to fight against terrorism.
Mr Zapatero's election victory was applauded by many Moroccans
"We are committed to boosting anti-terrorist co-operation," Mr Zapatero said after talks with King Mohammed VI and Prime Minister Driss Jettou.
His one-day visit comes as 14 Moroccans are being held in Spain on suspicion of involvement in the Madrid bombings.
It was the prime minister's first foreign visit since being appointed.
It has become a tradition for new Spanish prime ministers to make Morocco their first foreign trip.
After arriving, Prime Minister Zapatero was received by King Mohammed VI in the royal palace in Casablanca.
Then, in a park in the city centre, the two men unveiled a marble plaque dedicated to the memory of the 33 victims of a series of suicide bombs in Casablanca last year.
Some of the Moroccans being held in Spain are also suspected of having links to the Casablanca attacks.
After talks with both the king and the prime minister, Mr Zapatero told journalists that both countries had been "victims of horrendous attacks".
Morocco and Spain are co-operating in the investigation into the Madrid attacks and extending that co-operation was expected to be high on the agenda of Saturday's talks.
Relations between Spain and Morocco have often been strained - over fishing disputes, immigration and drug-trafficking, says the BBC's Katya Adler.
But new fears of cross-border terrorism have strengthened the resolve of both sides to co-operate, she says.
The visit is "a reunion of two countries which, although they are separated by just a few miles of water, have been too far apart recently because of reproaches and unnecessary tensions", Spanish Deputy Prime Minister Maria Teresa Fernandez de la Vega reportedly told reporters on Friday.
"Morocco and Spain are showing with this meeting that there are not two opposing civilisations, the Arab civilisation and the Western civilisation, but there is a single civilisation which confronts the barbarism of terrorism firmly," she said.
There was almost a military clash in 2002 over a Mediterranean island
The BBC's Pascale Harter in Casablanca says relations between the two countries are now better than ever.
Correspondents say many Moroccans perceived Spain's previous Prime Minister Jose Maria Aznar as hostile and contemptuous to their country.
But they have given an enthusiastic welcome to Mr Zapatero.
"Zapatero: the Spain that we love," said the front page of Moroccan daily Aujourd'hui Le Maroc.
"Zapatero is a great man," Moulay Larbi Alaoui, the father of a victim of the Casablanca attacks, told AP news agency.