A Shiite Moslem gunman, who has been indicted on charges of attempted murder and whose release is sought by the hijackers of the TWA aircraft, told a Madrid court today that he had fired on a Libyan diplomat's car here last year as "a slave of God."

Mohammed Jahir Abbas Rahal, 22, admitted in testimony that he had fired shots at the car of envoy Mohammed Idris Sept. 12. He said that he and an accomplice, fellow Lebanese and co-defendant Mustafa Ali Jalil, 24, were members of the Shiite Amal organization and that they had both received orders from their superiors to act against the Libyan target.

The trial date had been fixed last month but it has coincided with the hijack developments and with a specific demand, relayed by Amal leader Nabih Berri to the Spanish ambassador in Beirut, that the two accused be released as part of an exchange for the lives of the kidnaped TWA passengers.

Rahal and Jalil, both brought up in west Beirut, were arrested shortly after the shooting, in which the diplomat was wounded in an arm. He escaped his attackers and was in court today.

A policeman, called as a prosecution witness, said he had questioned Rahal and Jalil immediately after their arrest and that both had "continually expressed pride in what they had done." The state prosecutor is demanding 36-year jail sentences for them.

Questioned about the shooting, Rahal, who like Jalil spoke through an interpreter, said, "We are not free, we are the slaves of God."

The Spanish government has refused the demands for the release of Rahal and Jalil. There have been suggestions from officials that, should the two Shiites be found guilty and be sentenced by the Madrid court, they could be sent to Lebanon to serve the sentence.