You realize, of course, that it is possible to build a building that rotates with modern technology? I imagine a circular or rectangular building with one or more domes and a mihrab niche, so the building can be rotated so that the mihrab always points in the direction toward Earth.
And there could be a slot in the dome and in the mihrab niche in the walls with many sections of coverings that could be opened or closed so that there would be one opening in the slot centered on the dot that was Earth in the sky, or else pointed downward if Earth was below the horizon.
On Earth Muslim prayers orient themselves in the direction to Mecca, and stand, sit, bow, and prostrate themselves face downwards during different parts of the prayers. If they are facing the direction of Mecca, they may have their eyes looking in the direction toward Mecca when standing up during prayer sessions, and then have their eyes looking downwards at about 90 degrees from the direction of Mecca when prostrate face down during prayer sessions.
Since Mars orbits outside Earth, and since Islamic prayers are mostly during the daytime, Earth should always look somewhat close to the Sun during prayers.
According to an answer to this question, the maximum possible elongation between Earth and the Sun as seen from Mars is 47.378 degrees. So even at maximum elongation and in the worst possible configuration Earth will never be more than 47.378 degrees below the horizon during Martian daylight, and it will above the horizon during daylight about four times as often as it is below.
https://answers.yahoo.com/question/index?qid=20091003012620AAxZKSP1
I point out that on Earth, the orientation of a mosque is determined by the line to Mecca along the surface of the Earth, which of course is a curved line and not straight, even when it is calculated correctly. Hypothetical mosques that are exactly antipodal to Mecca will be oriented so that the Muslims will be looking along a line that travels 12,000 miles in a semicircle to Mecca, while the actual direction to Mecca is straight down from the mosque 8,000 miles through the center of the Earth.
By Muslim conventions, it is thus considered acceptable and usual for the mosque to be oriented so that the congregation faces up to 90 degrees away from the actual shortest, straightest, direct line to Mecca going through part of the Earth.
The 5 times Muslims on Earth pray are between dawn and sunrise, preferably 10 to 15 minutes before sunrise, then after true noon, then in the afternoon, then after sunset before dusk, and then after dusk.
If hypothetical future Martian Muslims pray during the Martian sol, or day, the Sun will always be over the horizon during the post noon and afternoon prayers. Since Earth will always be between 0 and 47.378 degrees from the Sun as seen from Mars, it can never be more than 47.378 degrees below the horizon during those prayer sessions.
Suppose that the Sun is high in the Martian sky, over 42.622 degrees high, and the Earth is 47.378 degrees above the Sun. Then the Earth will be over 90 degrees high, and thus behind the congregation. So the mosque will rotate 180 degrees until it is facing toward the Earth and Earth is high overhead and the congregation is facing in the direction to Earth.
During the dawn/sunrise and sunset/dusk prayer sessions, the Sun will be a few degrees below the horizon. If the Sun is X degrees below the horizon, and Earth is between zero and 47.378 degrees away from the Sun, the Earth can be as high as 47.378 minus X degrees above the horizon, or as low as 47.378 plus X degrees below the horizon. X should be only a few degrees, probably less than 10.
What about during the post dusk prayers? Then the Earth and Mecca could be as much as 90 degrees below the horizon. But never more than 90 degrees. If Earth is more than 90 degrees below the horizon the way the mosque is facing, the mosque can rotate until Earth is less than 90 degrees below the horizon.
On Earth Muslim prayers orient themselves in the direction to Mecca, and stand, sit, bow, and prostrate themselves face downwards during different parts of the prayers. If they have the horizontal direction to Mecca correct, their direction of vision will swing through about a 90 degree vertical arc several times during prayer that will pass each time through the exact direction to Mecca.
If a rotating Martian mosque prayer hall is correctly rotated so that its mihrab niche is facing toward Earth and Mecca in the horizontal plane, Earth and Mecca can never be more than 90 degrees (straight up or straight down) away from the mihrab in the vertical plane.
If Earth is exactly at the horizon during prayers, the congregation will face exactly toward Earth when standing and facing the mihrab. If Earth is below the floor, the congregation will briefly be looking exactly at Earth whenever they change from standing or sitting to bowing or prostrating themselves, and vice versa, and change the elevation they are looking at.
If Earth is high up in the sky during prayers, the congregation might not look exactly toward Earth during the prayers. And to avoid that, some Martian Muslims might hold mirrors at an angle and look up at the dome in the mirrors during part of the prayers. Or some Martian Muslim congregations might add to the prayer ritual a moment of looking upwards, even if it hurts their necks a bit, at the position of Earth.
Thus a Martian Muslim congregation, in a rotating prayer hall correctly aligned, can never be looking more than 90 degrees away from Earth, Mecca, and the Kaaba. And an Earthly Muslim congregation, in a stationary prayer hall with the mihrab pointed at Mecca, can never be looking more than 90 degrees away from Mecca and the Kaaba.
So as far as I can tell a horizontally rotating prayer hall on Mars would enable Martian Muslims to face in the direction of Mecca just as accurately as Muslims on Earth do.
Except that on Earth the line to Mecca is never a straight line directly to Mecca but always a curved line arcing along the surface of the Earth, while a line from a Martian mosque to Mecca on Earth would be a straight line.
Thus hypothetical Martian Muslims might consider Muslims on Earth to be some sort of heretics who don't pray correctly, and might invade Earth and force all Muslims on Earth to move to Mars so they could pray correctly, never being allowed to return to Earth except on pilgrimage to Mecca.
Of course the larger a building is, the harder it is to make to make it rotate.
Thus a Martian mosque for a large congregation might have to have many different prayer halls that rotate separately to keep lined up with Earth and Mecca.
Here is a list of some rotating buildings:
https://weburbanist.com/2008/09/20/rotating-architecture-16-sweet-spinning-structures/2
I note that each of the 11 floors, each with a single apartment, in the Suite Vollard, built 2001, in Curitiba, Brazil, rotates separately from the other floors.
https://www.emporis.com/buildings/136410/suite-vollard-curitiba-brazil3
A hotel planned for Dubai, scheduled to open in 2020, will have 80 floors with an apartment in each separately rotating floor.
https://www.news.com.au/travel/travel-ideas/weird-and-wacky/dubai-is-planning-the-worlds-first-fully-rotating-skyscraper/news-story/11b2f45c2137ac2bcd114c6b89ec70594
Wikipedia has a list of rotating restaurants subdivided into list for 69 countries.
The following is a list of revolving restaurants. A revolving restaurant is usually a tower restaurant (an eating space) designed to rest atop a broad circular revolving platform that operates as a large turntable. The building remains stationary and the diners are carried on the revolving floor.
https://en.wikipedia.org/wiki/List_of_revolving_restaurants5
Or possibly most of the building structure would be solid and unmoving, but a strip of wall perhaps ten meters tall, would be transparent, supported by widely spaced steel columns, and an inner opaque ring of wall just inside it would rotate so that the only transparent section of the wall, with architectural framing like a niche, would face toward Earth. That might save on the weight of structure that would have to be moved.
And of course legend tells of the amazing Takht-i-Taqhis built by Khusrau II, King of Kings of Iran, (reigned AD 590-628) which had a dome that revolved like the heavens, and the entire building could be turned in various directions.
And then there was Nero's Golden House. Seutonius, in his life of Nero, # 31 says:
The main banquet hall was circular and constantly revolved day and night, like the heavens.9
http://penelope.uchicago.edu/Thayer/E/Roman/Texts/Suetonius/12Caesars/Nero*.html6
And archaeologists discovered what could be the foundations of the revolving room in 2009.
http://www.ancient-origins.net/news-history-archaeology/revolving-dining-room-emperor-nero-s-luxurious-palace-really-existed-0018247
So people have been working on the design of revolving structures before the first mosques were ever built, and for two thousands years before the probable date for the first colonization of Mars.