Is it secure to use Diffie-Hellman key agreement to generate a nonce? I have a system, using AES, in one of the modes that uses a nonce and authentication.

We have a pre-shared key, and to agree about initial nonce we could use Diffie-Hellman, using the resulting secret as an IV.

I want to know if there can be any attack on this system: is it possible to perform a man-in-the-middle attack or a replay attack?
This algorithm is vulnerable to a Man in the middle. From Wikipedia:

In the original description, the Diffie–Hellman exchange by itself does not provide authentication of the communicating parties and is thus vulnerable to a man-in-the-middle attack. Mallory may establish two distinct key exchanges, one with Alice and the other with Bob, effectively masquerading as Alice to Bob, and vice versa, allowing her to decrypt, then re-encrypt, the messages passed between them. Note that Mallory must continue to be in the middle, transferring messages every time Alice and Bob communicate. If she is ever absent, her previous presence is then revealed to Alice and Bob. They will know that all of their private conversations had been intercepted and decoded by someone in the channel.
A method to authenticate the communicating parties to each other is generally needed to prevent this type of attack. Variants of Diffie–Hellman, such as STS protocol, may be used instead to avoid these types of attacks.